Object oriented programming in Python under of
- 2020-05-09 18:45:55
- OfStack
inheritance
Inheritance describes how properties of a base class are "inherited" to a derived class. A subclass can inherit any property of its base class, whether it is a data property or a method.
The syntax for creating a subclass looks the same as for a normal (newfangled) class, with one class name followed by one or more parent classes that need to be derived from it:
class SubClassName (ParentClass1[, ParentClass2, ...]):
'optional class documentation string'
class_suite
The instance
class Parent(object): # define parent class Define the parent class
def parentMethod(self):
print 'calling parent method'
class Child(Parent): # define child class Define subclasses
def childMethod(self):
print 'calling child method'
Inheritance and coverage
inheritance
Unlike Java, a subclass of python inherits all of the parent's methods, including the constructor init().
class Parent():
def __init__(self):
print "init Parent class instance"
def func(self):
print "call parent func"
class Child(Parent):
def __init__(self):
print "init Child class instance"
child = Child()
child.func()
The output
init Child class instance
call parent func
super keyword
super is used to solve multiple inheritance problems. Calling the parent method directly with the class name is fine when using single inheritance, but using multiple inheritance involves lookup order (MRO), repeated calls (diamond inheritance), and so on. Syntax is as follows
super(type[, obj])
The sample
class C(B):
def method(self, arg):
super(C, self).method(arg)
Pay attention to
super inheritance can only be used for new-style classes, and it will report errors when used for classic classes.
New-style classes: you must have inherited classes, and if you don't have anything to inherit, you can inherit object
Classic class: no parent class. If you call super, you get an error: "super() argument 1 must be type, not classobj"
The instance
class Parent(object):
def __init__(self):
self.phone = '123456'
self.address = 'abcd'
class Child(Parent):
def __init__(self):
super(Child, self).__init__()
self.data = 100
def main():
child = Child()
print "phone is: ", child.phone
print "address is: ", child.address
print "data is: ", child.data
if __name__ == '__main__':
main()
The output
phone is: 123456
address is: abcd
data is: 100
rewrite
A subclass can override a parent class method by redefining a method with the same name as the parent class method. A subclass can simply override func(self) of the parent class above.
class Parent():
def __init__(self):
print "init Parent class instance"
def func(self):
print "call parent func"
class Child(Parent):
def __init__(self):
print "init Child class instance"
child = Child()
child.func()
The output
init Child class instance
call Child func
Multiple inheritance
Like C++1,Python allows subclasses to inherit from multiple base classes. However, multiple inheritance is generally not recommended for 1. The syntax is as follows:
class Father():
def __init__(self):
print "init Father instance"
class Mother():
def __init__(self):
print "init Mother instance"
class Child(Father, Mother):
pass
Built-in functions for classes, instances, and other objects
issubclass()
Boolean functions determine that a class is a subclass or descendant of another class. It has the following syntax:
issubclass(sub, sup)
isinstance()
Boolean functions are useful in determining whether an object is an instance of another given class. It has the following syntax:
isinstance(obj1, obj2)
attr() functions
Low hasattr ()
Its purpose is to determine whether an object has a specific property. 1 is usually used to check 1 before accessing a property.
Low getattr () and setattr ()
● the getattr() and setattr() functions get and assign properties to the object accordingly,
Low delattr ()
Delete a specific property
The instance
class Child(Parent):
def __init__(self):
self.data = 100
child = Child()
print "has data attr?", hasattr(child, 'data')
print "delete attr"
delattr(child, 'data')
print "has data attr?", hasattr(child, 'data')
print "set data attr to 200"
setattr(child, 'data', 200)
print "data attr is: ", getattr(child, 'data')
The output
has data attr? True
delete attr
has data attr? False
set data attr to 200
data attr is: 200
privatisation
Python is not really encapsulated like Java, but is privatised with double and single underscores.
Low double
Prevent external access. If you double-underline func, you can prevent access to instances that include subclasses.
def __func(self):
print "call"
Low single
Prevent module properties from being loaded with "from mymodule import *".