Python basic 09 Object-oriented further development

Source: Internet
Author: User

We are familiar with the basic concepts of objects and classes. We will further expand so that we can actually use objects and classes.

Calling additional information for the class

As mentioned in the previous note, when defining a method, you must have the self parameter. This parameter represents an object. Object has all the properties of a class, we can invoke the class property through self.

class Human (object):     ' Hahahaha '    def Show_laugh (self):         Print Self.laugh     def laugh_100th (self):          for  in range:            Self.show_laugh ()

Li_lei = Human ()
li_lei.laugh_100th ()

Here is a class attribute laugh. In Method Show_laugh (), the value of the property is called through Self.laugh.

Other methods can also be called in the same way. Method Show_laugh (), which is called in Method laugh_100th ().

You can modify class property values through an object. But it's dangerous. The Class property is shared by all objects of the same class and its subclasses. Changing the value of a class property affects all objects.

__init__ () method

__init__ () is a special approach (special method). Python has some special methods. Python treats them in a special way. The special method is characterized by two underscores before and after a name.

If you define the __init__ () method in your class, Python automatically calls this method when you create the object. This process is also called initialization .

class Happybird (Bird):     def __init__ (self,more_words):         Print ' We are happy birds. ', more_words 
Summer = Happybird (' happy,happy! ')

The bird class is inherited here, and its definition is shown in the previous speech.

On-screen printing:

We are happy birds. happy,happy!

We see that although we just created the summer object, the __init__ () method is called automatically. The last line of the statement (Summer = Happybird ...) The object is created first and then executed:

summer.__init__ (More_words)

The ' happy,happy! ' was passed to the __init__ () parameter more_words

the nature of the object

We've talked about many properties, but these are properties of the class . All objects that belong to the class share these properties. For example, birds have feathers and chickens can't fly.

In some cases, we define the nature of the object, which is used to record special information about the object. For example, people in this class. Sex is a person's nature, not all human beings are men, or all are women. The value of this property varies with the object . Li Lei is a human object, gender is male, Han Meimei is also a human object, gender is female.

When you define a method for a class, you must pass a self argument. This parameter refers to an object of the class. We can modify the nature of an object by manipulating self. For example, to create a new object with a class, that is, li_lei in the example below, then Li_lei is represented by self. By assigning to Self.attribute, we add some properties to the object of Li_lei, such as gender. Self is passed to each method. Inside a method, you can query or modify the nature of an object by referencing Self.attribute.

In this way, in addition to the class attributes , each object is added to the nature of their own characteristics , so as to describe a diverse world.

class Human (object):     def __init__ (self, input_gender):         = Input_gender    def  Printgender (self):        print= Human ( ' male '  # Here, ' Male ' is passed as a parameter to the Input_gender variable of the __init__ () method. 
Print Li_lei.genderli_lei.printGender ()

In initialization, the parameters are Input_gender and assigned to the properties of the object, i.e. Self.gender.

Li_lei has the object nature gender. Gender is not a class property. After creating the Li_lei object, Python uses the object nature of Li_lei.gender to store unique information that belongs to the object Li_lei.

The nature of an object can also be called by other methods, similar to the invocation of a class property, as in the Printgender () method.

Summary

Invoking a class property through self

__init__ (): Automatically executes when an object is created

The difference between a class property and the nature of an object

Python basic 09 Object-oriented further development

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.