Object oriented programming in Python on of
- 2020-05-09 18:46:11
- OfStack
Create a class
The Python class is created using the class keyword. Simple class declarations can be keyword followed by class name:
class ClassName(bases):
'class documentation string' #' Class document string '
class_suite # The class body
instantiation
Instantiate a class by the class name followed by a pair of parentheses
mc = MyClass() # instantiate class Initialize the class
' int()' The constructor
def __int__(self):
pass
Note :self is similar to Java's this keyword in that the code points to a reference to its own instance
Class attribute
Unlike object-oriented languages like Java and C++, python's attributes include both data members and function elements, accessed via period symbols.
Special data built-in properties
C.name class C name (string)
The document string for the C.doc class C
A tuple formed by all parent classes of the C.bases class C
C.dict class C properties
C. module class C defines the module (new in version 1.5)
C.class instance C corresponding class (in new-style class only)
Special method built-in properties
dir(): gets a list of class or instance property names.
Static variable attribute
Define directly in the class scope
class C(object):
foo = 100
Instance variable properties
The instance properties of python are different from those of Java and C++. In Java and C++, instance properties must be declared/defined first, while python instance properties are created dynamically. Setting the properties of an instance can be done at any time after the instance is created, or in code that has access to the instance. structure
init() is one of the key points for setting these properties.
def __init__(self, name, data):
self.name = name
self.data = "123'
Note :self is similar to Java's this keyword in that the code points to a reference to its own instance
The method attributes
It is divided into instance method and class method. Class methods are owned by both the class and the instance.
Instance methods
class MyClass(object):
def myNoActionMethod(self):
pass
Note :self is similar to Java's this keyword in that the code points to a reference to its own instance
A static method
Static methods are class-level methods that can be called without instantiating a class. There are two method definitions
● decorator (commonly used)
@staticmethod
def foo():
print 'call static method'
● built-in functions
def foo():
print 'call static method'
foo = staticmethod(foo) # A static method
Class method
A static method is a class-level method, which, unlike a static method, must display the cls class parameter passed in. And if you need to call any other static method in your class, or a function of a class method, define it as a class method.
● decorator (commonly used)
@classmethod
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
● built-in functions
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
bar = classmethod(bar) # Class method
Example explanation
#!/usr/bin/python
#coding=utf-8
class Target(): # Define the class Target
'This is Target definition' # define __doc__ attribute
staticVar = 'v1.0' # Define static variables
def __init__(self, name = 'default', data = 0): # Define the constructor
self.name = name # The instance variables
self.data = data # The instance variables
print "init instance"
def main():
print "this is a test function"
'''
You can use decorators to define static methods
@staticmethod
def foo():
print 'call static method'
'''
def foo():
print 'call static method'
foo = staticmethod(foo) # A static method
'''
You can define class methods with decorators
@classmethod
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
'''
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
bar = classmethod(bar) # Class method
# Only when this module is called main() The method takes effect
if __name__ == '__main__':
main()
# instantiation
target = Target('aaa', 123)
print 'name is: ', target.name
print 'data is: ', target.data
# print __doc__ attribute
print 'target.__doc__ is: ', target.__doc__
# Print the class __dict__ attribute
print 'Target.__dict__ is: ', Target.__dict__
# Print static variable
print 'staticVar is: ', Target.staticVar
# Print the built-in function dir()
print 'dir() is: ', dir(Target)
# Calling static methods
Target.foo()
# Calling class methods
Target.bar()
The output
mc = MyClass() # instantiate class Initialize the class
' int()' The constructor
def __int__(self):
pass
0