"Programming Little White's first Python primer" note four

Source: Internet
Author: User
Tags instance method

Seventh class and Coca-Cola

7.1 Defining a class

For example :class CocaCola:

& nbsp;       formula = [ ' caffeine ' , ' sugar ' , ' water ' , ' soda ' ]

Defining a class using class is as simple as defining a function when creating a function, and then you can see that there is a variable loaded with a list in the indentation, the variable that is assigned to the class is the variable of the formula class, and the variable of the class has a proprietary term that we call the Class property ( Class Atrribute)

Instantiation of Class 7.2

coke_for_me = CocaCola()

On the left we create a variable with the name of the class on the right, which looks much like the behavior of the assignment, which we call the instantiation of the class

Class 7.3 Property Reference

After the name of the class is entered . , the IDE automatically reminds us of the properties we wrote in the previous definition of the class, and this is the reference to the Class property (attribute References).

The properties of the class are shared by instances of all classes , so when you click on the instance of the class . , the value of the property referenced is exactly the same.

7.4 Instance Properties

Through object.new_atrr the form of an assignment, we get a new instance of the variable, the instance variable is an instance variable, and the instance variable has a proprietary term that we call the instance attribute (Instance Atrribute)

7.5 Example Methods

An instance of a class can reference a property, but we learned earlier that an instance of the class could use the method (see chapter III: Methods of Strings). The method is the function, but we call this function method. Methods are for instance use, so we can also call them instance methods (Instance method)

7.6 More parameters

Like functions, class methods can have their own parameters.

7.7 Magic Methods

__init()The magic of this is that if you define it in a class, it can help you handle a lot of things automatically when you create an instance.

Inheritance of Class 7.8

The reference mechanism for a property in Python is Alien, and when you create an instance, you are ready to start referencing the property, and the compiler will search for the attribute if it has one, and if it does, it will search for the class to which the instance belongs, and if so, the reference, No, that would only be an error.

"Programming Little White's first Python primer" note four

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.