Python3 class and object: Collection, python3 object collection

Source: Internet
Author: User

Python3 class and object: Collection, python3 object collection
1. Combination

1. Method:In a new class definition, you can put the required class into the instance.

Example:

// Tortoise class Turtle: def _ init _ (self, x): self. num = x // Fish class Fish: def _ init _ (self, x): self. num = x // Pool class Pool: def _ init _ (self, x, y): self. turtle = Turtle (x) // combines the tortoise class into self. fish = Fish (y) // combine fish into def print_num (self): print ("total turtles % d in the pool, Fish % d! "% (Self. turtle. num, self. fish. num)> pool = Pool (1, 10)> pool. print_num ()

 

2. When to use a combination and when to use inheritance:

When a class is similar to a class, a new class can be used to inherit some classes (parallel relationships)

When you need a new class that contains an existing class, you need to use a combination, just like the relationship between the fish and turtles in the pond (for your own understanding, please note if you have any questions)

2. Generation of class objects

After a class is defined, the class definition becomes a class object. Class object can be directly represented by class name. It has two types of operations: direct class attribute operation and instantiation. You can directly use the "class name. Attribute" method to use class attributes. To use class methods, you need to instantiate an object to use it. That is, you need to pass in a self parameter to use it.

Example

Class C: # create a new C class t = 0 # class attribute def x (self): # class method print ('xman ') >>> C. t # class name (Class Object) can be used to directly call class property 0 # However, if you use Class Object to call a method, an error is reported because the self parameter is missing, therefore, class methods must be instantiated before they can be called> C. x () Traceback (most recent call last): File "<pyshell #1>", line 1, in <module> C. x () TypeError: x () missing 1 required positional argument: 'Self '# Call the class method after instantiation >>>> a = C () >>>. x () Xman
3. The property name and method of the object should be different. If the property with the same name overwrites the Method
class C:        def x(self):                print('Xman')>>> c = C()>>> c.x()Xman>>> c.x = 1>>> c.x1>>> c.x()Traceback (most recent call last):  File "<pyshell#20>", line 1, in <module>    c.x()TypeError: 'int' object is not callable
Example:

Defines how many instances of A Class Object and class are created.

Class C: count = 0 def _ init _ (self): self. count + = 1 # change self to C def _ del _ (self): self. count-= 1 # self can be changed to C >>> a = C () >>>. count1> del a> C. count0

 

Related Article

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.