Home > other >  Python class instantiation, do not add parentheses after the name of the class is what mean?
Python class instantiation, do not add parentheses after the name of the class is what mean?

Time:09-28

For example: the following code
 
# Instantiate FTP handler class
Handler=FTPHandler
Handler. The authorizer=authorizer

Not should add parentheses after FTPHandler?

CodePudding user response:

For example you see which is instantiated:

 class clsTest () : 
Y='
Def __init__ (self) :
The self. The y='you'

X=clsTest
Print (x.y)

X=clsTest ()
Print (x.y)

CodePudding user response:

reference 1st floor chuifengde response:
, for example, you see which is instantiated:

 class clsTest () : 
Y='
Def __init__ (self) :
The self. The y='you'

X=clsTest
Print (x.y)

X=clsTest ()
Print (x.y)

Add parentheses is class instantiation, what on earth is that do not add parentheses, like I suggested that code, don't understand what you mean

CodePudding user response:

Do not add parentheses is not restart the instance, this should be set an alias is the same as
Handler. The authorizer is actually FTPHandler sharing members of a class

CodePudding user response:

refer to the second floor HowieZhao response:
Quote: refer to 1st floor chuifengde response:

For example you see which is instantiated:

 class clsTest () : 
Y='
Def __init__ (self) :
The self. The y='you'

X=clsTest
Print (x.y)

X=clsTest ()
Print (x.y)

Add parentheses is class instantiation, what on earth is that do not add parentheses, like I put the code and I don't know what do you mean


To put it bluntly bracketing is calling the init function without parentheses is not call the init function

CodePudding user response:

You can print out the two instantiate type, do not add parentheses is equivalent to set an alias, its type should be & lt; The type 'classobj & gt;; But add parentheses is the right way to instantiate, printing type should be & lt; The type 'instance & gt;

CodePudding user response:

CodePudding user response:

Personally, I think python is a reference mechanism, do not add parentheses rather then a variable pointing to the class in the space, there are brackets are to perform

CodePudding user response:

 
The class clsTest () :
Y='
Def __init__ (self) :
The self. The y='you'

> Type (b)
> C=clsTest ()
> Type (c)

The above code, is a class b,
B.a uthorizer=authorizer
In fact is for class attr assignment,

CodePudding user response:

Unable to edit out a reply,,,,,,,
-- -- -- -- -- -- -- -- -- --

 
The class clsTest () :
Y='
Def __init__ (self) :
The self. The y='you'


> B=clsTest
> Type (b)
> C=clsTest ()
> Type (c)
& lt; The class '__main__. ClsTest & gt;

The above code, is a class b,
B.a uthorizer=authorizer
In fact is for class attr assignment,

CodePudding user response:

Python everything object, class itself is object, do not add parentheses is object reference

CodePudding user response:

In short, do not add parentheses is assignment,
1st floor of the case, call x and call clsTest is the same,

CodePudding user response:

CodePudding user response:

Add parentheses is instantiated, init magic methods will call after listening to instantiate, do not add parentheses, is called the class itself,
  • Related