Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Studying(4) #4

Open
tushao opened this issue Jan 29, 2015 · 0 comments
Open

Python Studying(4) #4

tushao opened this issue Jan 29, 2015 · 0 comments

Comments

@tushao
Copy link
Owner

tushao commented Jan 29, 2015

Implicit Inheritance:
class Parent(object):

def implicit(self):
    print "PARENT implicit()"

class Child(Parent):
pass

dad = Parent()
son = Child()

dad.implicit()
son.implicit()
The use of pass under the class Child: is how you tell Python that you want an empty block. This creates a class named Child but says that there's nothing new to define in it.

Override Explicitly
Alter Before or After:hen you use a Python built-in function named super to get the Parent version to call

When to Use Inheritance or Composition:
Avoid multiple inheritance at all costs, as it's too complex to be reliable. If you're stuck with it, then be prepared to know the class hierarchy and spend time finding where everything is coming from.
Use composition to package code into modules that are used in many different unrelated places and situations.
Use inheritance only when there are clearly related reusable pieces of code that fit under a single common concept or if you have to because of something you're using.

chapter46,47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant