Inheritance relationships for Python classes
Class Studeent (object):
"" "DocString for Studeent" ""
def Init(self, arg):
Super (studeent, self). Init ()
Self.arg = arg
S1=studeent (23)
Print S1.arg
S1.name= ' Zhangfei ' #自由的给对象绑定属性
Print S1.name
Class Teacher (object):
"" "DocString for Teacher" ""
def Init(self,name,age):
Super (teacher, self). Init ()
self.name=name self.age=agedef print_info(self): print(‘%s: %s‘ % (self.name, self.age))
T2=teacher (' I am ', 23)
Print T2.age
T2.print_info () #类中定义的函数只有一点不同, that is, the first argument is always the instance variable self, and, when called, does not pass the argument
Class Animal (object):
def run (self):
Print (' Animal is running ... ')
Class Dog (Animal):
def run(self): print(‘Dog is running...‘)
Class Cat (Animal):
def run(self): print(‘Cat is running...‘)
def run_twice (animal):
Animal.run ()
Animal.run ()
Run_twice (Animal ())
Run_twice (Cat ())
Class Tortoise (Animal):
def run (self):
Print (' Tortoise is running slowly ... ')
Run_twice (Tortoise ())
#静态语言 vs Dynamic Language for static languages (for example, Java), if you need to pass in a animal type, the incoming object must be a animal type or its subclass, otherwise the run () method cannot be called. For dynamic languages such as Python, it is not necessarily necessary to pass in the animal type. We just need to ensure that the incoming object has a run () Method: Class Timer (object):d EF run (self):p rint (' Start ... ') This is the "duck type" of the dynamic language, which does not require a strict inheritance system, an object as long as " It looks like a duck and walks like a duck, so it can be seen as a duck. Python's "File-like object" is a type of duck. For a real file object, it has a read () method that returns its contents. However, many objects, as long as there is a read () method, are considered "File-like object". Many functions receive parameters that are "File-like object", you do not have to pass in a real file object, you can pass in any object that implements the Read () method. #
Python Regular Expressions