This article describes the Python object-oriented objects like class and basic usage, detailed analysis of the Python object-oriented programming class class and object basic concepts, principles, methods and operational use precautions with examples in the form of a friend in need can refer to the
examples herein describes the basic usage Python object-oriented class and like objects. Share to you for your reference, as follows:
Class (class): the definition of a thing abstract characteristics, usually, class defines the properties of things and it can be done for the sex
Objects (object): are instances of classes.
1. basic points
class MyClass(object):
message = "hello,world"
def show(self):
print (self.message)
There is a class called MyClass member variable: message, and given the initial
class member functions defined in the show (self), note that the class member function with parameters must be self
argument self is a reference to the object itself, can be a member function self reference parameter information obtained object
Output:
inst = Myclass() # 实例化一个MyClass 的对象
inst.show # 调用成员函数,无需传入self参数
hello,world
Note: The parentheses behind by the class name may be obtained directly instantiated class object variable, using the member functions of the object variables can access the class member variables.
2. Constructor
A constructor is a special member of the class methods, mainly used to create the object initialization, class constructor python with the named __init__:
class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print self.message
def __init__(self):
print "Constructor is called"
inst = MyClass()
inst.show()
>>>
Print Results:
>>>Constructor is called
>>>Hello, Developer.
NOTE: constructor can not have a return value, Python not define multiple constructors, but can be configured in various ways to achieve the object of the object through the provision of default values for the named parameters.
3. destructor
It is to construct a reverse function that is called when they destroyed or release objects.
python class definition for the destructor method defined function has no return value and parameters of __del__ named in the class definition.
class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print self.message
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
def __del__(self):
print "Destructor is called!"
inst = MyClass()
inst.show()
inst2 = MyClass("David")
inst2.show()
del inst, inst2
inst3 = MyClass("Lisa", "Yellow")
inst3.show()
del inst3
>>>
Print result:
4. instance member variables
Constructor defined variables referenced self, and therefore such a member variable called instance variables in python member.
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.name = name
self.color = color
The static functions and functions:
python supports two functions based on the class name to access members: static function, class functions.
Except that: there is a hidden class of functions can be used to obtain parameters cls class information. The static function without this function.
Static function with decorator: @staticmethod defined
class function decorator: @classmethod defined
class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print (self.message)
print ("Here is %s in %s!" % (self.name, self.color))
@staticmethod
def printMessage():
print ("printMessage is called")
print (MyClass.message)
@classmethod
def createObj(cls, name, color):
print ("Object will be created: %s(%s, %s)"% (cls.__name__, name, color))
return cls(name, color)
def __init__(self, name = "unset", color = "black"):
print ("Constructor is called with params: ",name, " ", color)
self.name = name
self.color = color
def __del__(self):
print ("Destructor is called for %s!"% self.name)
MyClass.printMessage()
inst = MyClass.createObj( "Toby", "Red")
print (inst.message)
del inst
Output:
6 private member
python using the specified variable name format method definition private members, that is, all double underscore "__" start naming members to private members.
class MyClass(object):
def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.__name = name
self.__color = color
def __del__(self):
print "Destructor is called for %s!"% self.__name
inst = MyClass("Jojo", "White")
del inst
Output:
recommended Our python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering