Python Regular Expressions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.