C ++ programmers learn Python: Object-oriented in Python

Source: Internet
Author: User

In python, object-oriented methods are also introduced. Apart from some basic concepts of C ++, there are other differences. For example, Python classes can sometimes be used as namespaces.

 
1 ClassMydata (object ):2Pass

The inheritance Syntax of python is class class_name (parent_class_name). Therefore, the above mydata object inherits from the object. As for the object, it is a python built-in object.

 

The preceding statement declares an empty class, but unlike C ++, this class can be used as a namespace. For example:

 
1Mathobj =Mydata ()2Mathobj. x = 43Mathobj. Y = 54Mathobj. x + mathobj. Y

 

In this case, mathobj is only used as a namespace.

When we need to define a member function:

 
1 ClassMydata (object ):2DefPrintfoo (Self ):3Print "You invoked printfoo ()"4DefPrintfoo2 ():5Print "You invoked printfoo2 ()"

Printfoo2 is incorrectly defined. When the member function printfoo2 is called, the interpreter reports an error because it defines the form parameter self, and self is the this in C ++, however, when we define a member function in C ++, it will automatically generate the function for us, instead of manually generating it like python (or I don't know ??), We call a member function. Both Python and C ++ will automatically help us to pass class objects. So when we call printfoo2, the interpreter will help us to pass the self object, however, printfoo2 does not define this parameter.

 

Of course, classes in Python also have constructor and destructor. Different from C ++, the python constructor is _ init __, the Destructor is _ del __

1 ClassMydata (object ):2Def _ Init __(Self ):3Print "Init"4Def _ Del __(Self ):5Print "Del"
 
 
1OBJ =Mydata ()2 Init3OBJ = 24 Del

When we create OBJ, we call the _ init _ function. When we direct OBJ to other data, Python starts the garbage collection mechanism and the mydata object is deleted, the _ del _ function is also called.

 

In python, another note is that an instance with the same name will be created when the class is created. For example, we have the following classes:

 
1 ClassC (object ):2Foo = 100.

If we enter print C. foo, the interpreter outputs 100. If we input C. foo = C. foo + 1, then C. foo will change to 101. At this time, if we are B = C (), then print B. foo will find that the output is 101, because foo is actually a static variable here, similar to C ++ defining a static int Foo in the class

However, if a function is defined in C, what will happen when we call C. function ()? An error is reported. This is different from Foo because the function has not been bound to a class.

 

In C ++, if we need to obtain information about a class, we can use typeof, and Python also has similar functions. However, classes must inherit from objects.

There are the following:

_ Name __: Class Name

_ Doc __: class document

_ Bases __: tuples of all parent classes

_ Dict __: class attributes

_ Module __: module of the class

_ Class __: corresponding class

 

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.