1, static methods: only nominally classified to manage, in fact, can not access any property or class instance in a static method.
(1) we look at the following code, an object is instantiated, d can directly talk call ()
class Dog(object): def __init__(self,name): self.name=name def talk(self): print("%s is talking" % self.name) d=Dog("chen") d.talk()
(2) and after we use the static method, instantiated directly call if d.eat () will complain, because in a static method, instantiated name is passed does not go eat in the function;
Dog class (Object):
DEF __init __ (Self, name):
self.name = name @staticmethod # static methods with no real class relations, but must go through the class name to call; call not class variables with instance variables, the equivalent of just a function class
def eat (self): # passed after instantiation is not coming, passed in if you want, you need to be passed in the d
print ( "% s is eating% s"% (self.name, ' buns'))
D = Dog ( "Chen")
d.eat () # error eat () missing 1 required positional argument : 'self', self instantiate the mass does not go
So this time if we want to access eat () method, you need to pass an instance to change the function, that is,
d.eat(d)
Such an object can be instantiated in the d name "chen" transfer function to eat.
2. Class Methods
Class Methods: You can only access class variables, instance variables can not be accessed.
As the following procedure, we will eat () method is defined as a class method, then found in the object instance, the method given call eat, then add self.n = 333 variables in the constructor, performs error still described class methods can not access instance variables
Dog class (Object):
DEF the __init __ (Self, name):
the self.name name =
self.n = 333 @classmethod # class methods, class variables can only access, can not access instance variables
def eat (self): # instantiated after the transfer is not coming, passed in if you want, you need to be passed in the d
print ( "% s is eating% s"% (self.name, ' buns'))
Print ( "% S% S EAT iS" % (self.n, 'buns'))
D = Dog ( "Chen")
d.eat ()
And we add two class variables in Dod class, the next time you execute the program:
class Dog(object): n=333 name="hhh" def __init__(self,name): pass
From the above, class methods can access only class variables
3. Properties Methods
Properties Methods: One way to become a static property,
(1) the following procedure:
Dog class (Object): DEF the __init __ (Self, name): the self.name name = @Property #attribute DEF EAT (Self): Print ( "% S% S IS eating"% (the self.name, 'buns')) Dog = d ( "chen") # d.eat () # error when such calls should d.eat d.eat property # method: a method to become a static property
At this point, eat method is static properties, so d.eat () will complain when you call, we need to call: d.eat
(2) A this time is an attribute eat, there is no way to pass parameters bracket, then if the passed parameters give eat, eat write a same function, then the transfer can pass in a parameter;
eat.setter @ DEF EAT (Self, Food): Print ( "the SET to Food:", Food) d = Dog ( "chen") d.eat # property method: a method into a static property d.eat = "Baozi" # If you want to pass parameters, you must write a same function, @ eat.setter
The above procedure the following output: see baozi is passed in this case
chen is eating 包子 set to food: baozi
B assigned to eat the above stored, found in the printed output when changes:
Dog class (Object): DEF the __init __ (Self, name): the self.name name = Self .__ = None Food @Property #attribute DEF EAT (Self): Print ( "% S% S IS eating"% (the self.name, Food .__ Self)) @ eat.setter DEF EAT (Self, Food): , Food): Print ( "SET to Food" Self .__ Food Food = D = Dog ( "Chen") d.eat # property methods of: a way into a static property d.eat = "baozi" # If you want to pass parameters, you must write a same function, @ eat.setter d.eat
The parameters passed to eat "baozi" survive: results are as follows
chen is eating None set to food: baozi chen is eating baozi
(3) Delete Attribute: write a same function, delete; then call d.eat error in
eat.deleter @ DEF EAT (Self): del Self .__ Food Print ( "Delete completed")
del d.eat
after d.eat # delete private property, when called again error
4. Special class members in the program :( Example 3)
(1) __ call __ () method
__Call__ brackets behind the object, trigger the execution.
The constructor is executed by the object triggers created, namely: class name = Object (); the method is performed for __call__ bracketed by the object triggers, namely: Object () or class () ()
Examples of d-Dog = d ( "Chen") , the re-direct d () error, then we write the __call a __ () method in class Dog,
class Dog(object): def __call__(self, *args, **kwargs): print("running call",args,kwargs) d=Dog("chen") d() #d=Dog("chen") d(),与Dog("chen")()一样 d(1,2,3,name=333)
Then see the output results:
running call () {} running call (1, 2, 3) {'name': 333}
(2) __ dict __ () method
When no instance of the object class method in the form of a printed dictionary, but does not include the attribute instance
Method #__dict__ print (Dog .__ dict__) # no instance in the form of a dictionary object class print out the method does not include the instance attribute
After instantiating the object instance attributes of print objects:
Dog = D ( "Chen") Print (D .__ dict__ magic) # call by way of example, only print all the instance variables
output is as follows:
{ 'name': 'Chen', '_Dog__food':} None
(3) __ str__ Method:
If we print directly instantiated, did not write str method, the output of the memory address
Dog = D ( "Chen") Print (D) # Without str method, printed as <__ main__.Dog object at 0x01104B70>; if str writing method, when the print target, the method returns a default output value
If we write the class Dog str method, after the instantiation print output for the return value
__ __str DEF (Self): return "<obj:% S>" the self.name%
D = Dog ( "Chen")
Print (D) str # If the write method, when the print target, the default output the return value : <obj: chen>