Python learning: custom object

Source: Internet
Author: User

Python learning: custom object

I. first look at a small program

Class person: def _ init _ (self): print "new person" p = person ();


It can be seen that the class custom object with the keyword is used in python, followed by a colon

Then we need to define the constructor _ init _ of the class. In the constructor, We need to input the self parameter. this self is equivalent to this in java and points to the object itself.

Then p = Person () creates an object, and the class construction method is executed.

Execution result:

New person


Ii. Define attributes of a class

Look at a small program:

Class person: def _ init _ (self): print "new person" self. name = "lyl" p = person (); print p. name

We can see that attributes can be defined in the constructor, and self

Then we print the name attribute of the created object.

Output result:

New person
Lyl


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.