Python learning notes (I) class and inheritance use, python learning notes inheritance

Source: Internet
Author: User

Python learning notes (I) class and inheritance use, python learning notes inheritance

I plan to learn Python a year ago. I have never been familiar with it, but I am not familiar with it. I wrote several network programming examples using Python yesterday, I felt like I was moving forward a lot. In the past few days, I 've sorted out my notes on learning Python. The content should be as concise as possible.

 

 

The following example shows the basic usage of the class:

# Coding: utf-8class Test (): s = 'This is a static variable 'def _ init _ (self): print' Here is the constructor 'self. a = 1 self. B = 12 def _ del _ (self): print 'Here is the Destructor 'def foo (self): print 'normal member function' @ staticmethod def bar (): print 'static function of the class 'if _ name _ = '_ main _': t = Test () t. foo () Test. bar () print t. _ class _ print Test. _ bases _ print Test. s

Many books introduce Python classes, but many Python learners already have the basics of C ++ or Java, so I tried to write this demo directly.

In Python, the constructor uses _ init __and The Destructor uses _ del __.

In C ++, the member variables and functions of the class are determined before compilation, while Python can be determined at runtime.

In the preceding example, s is equivalent to a static variable, and the entire class has the same property.

Self. a In the _ init _ function is a common member variable. If you use

self.c = 'foo'
Such statements mean that you have added a data member to the object in this row.

However, note that c is added to the data member of the object only after this row is run. Therefore, Python member variables can be increased or decreased during running.

 

 

 

Let's look at the second example, about inheritance and combination:

# coding:utf-8class Base():    def __init__(self, a, b):        print 'Base construct.'        self.a = a;        self.b = b;        self.t = Other()    def __del__(self):        print 'Base destroy.'    def foo(self):        print 'a = %s b = %s' % (self.a, self.b)class Other():    def __init__(self):        print 'Other construct.'    def __del__(self):        print 'Other destroy.'class Derived(Base):    def __init__(self, a, b):        Base.__init__(self, a, b)        print 'Derived construct.'    def __del__(self):        Base.__del__(self)        print 'Derived destroy.'if __name__ == '__main__':    d = Derived("foo", "bar")    d.foo()    print d.__class__    print d.__class__.__bases__    print Derived.__bases__

Base is a Base class, Derived inherits from Base, and an object of the Other class is a data member of Derived.

In this example, note that in the Derived constructor, you must manually call the Base constructor. The Destructor are also used in the same way.

 

In the last example, the function overwrite of the base class and the derived class is as follows:

# coding:utf-8class Foo():    def test(self):        print 'test in Base.'class Bar(Foo):    def test(self):        print 'test in Derived.'if __name__ == '__main__':    b = Bar()    b.test()

We run the program and print the following:

test in Derived.
It is found that the version of the derived class is called. This indicates that the test function of the derived class overwrites the version of the base class.

Note that, unlike C ++, the test function in Bar here does not matter even if the parameter is changed. In short, as long as the function and the function in the base class have the same name, that constitutes coverage.

If you want to call the base class version in the test function of Bar, you can use:

Foo.test(self)

The complete code is as follows:

# coding:utf-8class Foo():    def test(self):        print 'test in Base.'class Bar(Foo):    def test(self):        Foo.test(self)        print 'test in Derived.'if __name__ == '__main__':    b = Bar()    b.test()

 

.

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.