I java,net get started, of course Python also want to learn well, these days to introduce the Python object-oriented
Object-oriented programming requires the use of classes, classes and instances, and with classes we have to create an instance to invoke the method of the class. First look at the structure pattern of the class:
class<:
< Statement >
The private property of the class: __private_attrs the two underscore, declaring that the property is private and cannot be used or accessed directly outside the class. Methods used in internal methods of a class: Within the class, using the DEF keyword can define a method for the class, unlike a general function definition, the class method must contain the parameter self and the first argument, self.__private_attrs
The proprietary method of the class:
The __init__ constructor, which calls the __del__ destructor when the object is generated , uses __repr__ to print when the object is disposed, and
transforms the
__setitem__ by index
__getitem__
__len__ get length
__cmp__ comparison operation
__call__ function call
by index fetch value __add__ adding operation
__sub__ subtraction operation
__mul__ multiplication operation
__div__ addition operation
__mod__ calculation
__pow_ _ said Square
Building the constituent classes of a class:
#-*-Coding:utf-8-*-
#构建person的类
class person ():
name = " #首先定义一下变量 Age
= 0
__weight = 0
def __init__ (self,name,age,weight): #初始化变量
self.name = name #重新给类的name变量赋值, and can call
Self.age = age Globally
self.__weight = Weight
def __cmp__ (self):
pow_age = self.age.__pow__ (2)
print (pow_age)
def __len__ (self):
Name_del = self.name.__len__ ()
print (Name_del)
def __add__ (self):
adds = self.age.__add__ (self.__weight)
print (adds)
def infoma (self): #定义speak函数
print ('%s age%s weight%s ' % (Self.name,self.age,self.__weight))
Print (person.__class__)
print (person.__repr__)
<class ' type ' >
<slot wrapper ' __repr__ ' of ' object ' objects>
person = person (' Bruce ', 25,60) #将变量赋值给person类实例化
Print (person)
Infoma = Person.infoma () # Call the Infoma function method of the person class
<__main__.person object at 0x111dddba8>
Bruce age 25 body weight 60
Print (person.__class__)
print (person.__repr__)
<class ' __main__.person ' >
<method-wrapper ' __repr__ ' of person object at 0x111dddba8>
CMP = person.__cmp__ ()
lens = person.__len__ ()
adds = person.__add__ ()
print (' doc is%s '%person.__doc__) C10/>print (' dir is%s '%person.__dir__) print (' Delatter is%s
'%person.__delattr__)
print (' GT is%s '%person.) __gt__) print (' Hash is%s
'%person.__hash__) print ('
init is%s '%person.__init__ ')
print (' New is%s '% PERSON.__NEW__)
625
5
doc is None Dir-<built-in method __dir__ of the person
object at
0x111dddba8>
delatter I S <method-wrapper ' __delattr__ ' of person object in 0x111dddba8>
gt is <method-wrapper ' __gt__ ' of person obj ECT at 0x111dddba8>
Hash was <method-wrapper ' __hash__ ' of person object in 0x111dddba8>
init is <bound Method person.__init__ of <__main__.person object at 0x111dddba8>>
new are <built-in method __new__ of Typ E Object at 0x10f14a570>