Python basic to advanced functions of class 01, instance and static functions

python is a very simple and easy to use language, bloggers using python more than a year, but aware of their many characteristics of the python still not very familiar with many of the details did not grasp, even many do not understand do not know where 
github on python100days project I want to learn it again python, Network access it, this series of blog is used to record and share



#python in the class has three methods, class methods, instance methods, static methods and
# instance method is to create a new instance after the method can call the object's methods, and this java, c, etc. language or object is very similar to the method the first argument for the Self
# class method refers to the class attribute access methods, the first parameter is the default CLS, classmethod @
# static method is not a hidden first parameter, can be called directly, @ staticmethod

# 01 static methods
class Person (Object):

# define Person object can only bind _name, _age and _gender property, height can not be after removal __slot__ dynamic binding can be bound
__slots__ is = ( 'the _name', '_age', ''s _gender')

DEF the __init __ (Self, name, Age):
self._nam name = E
self._age = Age

@Property
DEF name (Self):
return self._name

@Property
Age DEF (Self):
return self._age

@ age.setter
DEF Age (Self, Age):
self._age = Age

DEF Play (Self):
IF self._age <= 16:
Print ( '% S is playing chess flight . '% self._name)
the else:
Print ('% S is playing doudizhu 'self._name%).

@staticmethod
DEF the printa ():
Print ( "A")

@classmethod
DEF new new (CLS, Age):
return CLS ( "Xiaoliu", Age)

# reason is to add a decorator for instance can call this fun, if not increase @staticmethod, can not be instantiated instance after calling printa but can not call
# fun because the instance needs afferent Self
@ staticmethod
DEF is_valid (A, B, C):
return A + B> C and B + C> A + C and A> B

the __name__ == IF '__main__':
Print (Person.is_valid (. 1, 2,. 3))
Person.printa ()
Person = the Person ( 'Xiaoliu', 22 is)
person.printa ()
# person._gender = "famale"
# = 168 person.height


# 02 example method, the first parameter is usually self, object instance represents relevant information, the object instance can be called instance methods, class instance can not call the method, because of the need to instantiate
# can call
person.play ( )
# class can not call an instance method
# Person.play ()

# method 03 class, first class method parameter convention called cls, it represents the current class of objects related information
# class method can help generate classes examples of directly determined by the class name is xiaoliu method, the age can be set by a method of the class, then a new instance of an
PERSON2 = Person.new (52 is)
Print (person2.name, person2.age)

person2.new (22 is)
Print (PERSON2 .name, person2.age)
# class class method may be called directly, or may be directly invoked instance of an object, the above statement person2.new (22) is not being given to prove that the class instance may call the function, but the xiaoliu
# Old change? The answer is no, because the return of cls ( "xiaoliu", age) does not correspond to an instance

person3 = person2.new (22)
Print (person3.name, person3.age)
# This person3 under the age would it be , the answer is 22, because cls information returned, used to instantiate a new instance of 3

Guess you like

Origin www.cnblogs.com/CooperXia-847550730/p/11365871.html