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