in the Python Development (http://www.maiziedu.com/course/python-px/) there is a function called a constructor in the programming language, and in this function there is a key word called self, and thefirst thing that is clear is that it will only be in the method of the class, independent function or method is not necessary with Self the. Let's talk about How to use the Self keyword in the constructor.
Structure
Class FooBar:
def _int_ (self):
Self.somevar = the
>>>f = FooBar ()
>>>f.somevar the
Rewrite
Class A:
def hello (self):
print "Hello, I ' m A"
Class B (A):
def hello (self):
print "Hello, I ' m B"
b = B ()
B.hello ()
Hello, I ' m B
Property
Class Rectangle:
def _init_ (self):
self.width = 0
self.height = 0
def setSize (sef, size):
Self.width, self.height = size
def getsize (self):
return self.width, Self.height
Use:
R = Rectangle ()
R . Width = Ten
R . Height = 5
R . GetSize () # (5)
R . SetSize (+- ) # (+)
Property function
is to wrap the attributes above:
Class Rectangle:
def _init_ (self):
self.width = 0
self.height = 0
def setSize (sef, size):
Self.width, self.height = size
def getsize (self):
return self.width, Self.height
Size = Property (GetSize, SetSize)
Use:
R = Rectangle ()
R . Width = Ten
R . Height = 5
R . Size # (5)
R . Size (+- ) # (+)
Static Methods
Adorner @
Class MyClass:
@staticmethod
def smeth ():
print ' This is a static method '
Python Constructor Learning Notes