The actual use of objects and classes in the Python Basics tutorial _python

Source: Internet
Author: User

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

Calling other information for a class

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

Copy Code code as follows:

Class Human (object):
Laugh = ' hahahaha '
def show_laugh (self):
Print Self.laugh
def laugh_100th (self):
For I in range (100):
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 by Self.laugh.

You can also invoke other methods in the same way. Method Show_laugh (), which is invoked in the method laugh_100th ().

You can modify class property values through objects. But it's dangerous. Class attributes are 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 the name.

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

Copy Code code as follows:

Class Happybird (Bird):
def __init__ (self,more_words):
print ' We are happy birds. ', more_words

Summer = Happybird (' happy,happy! ')


This inherits the Bird class, and its definition is shown in the previous lecture.

Print on screen:

Copy Code code as follows:

We are happy birds. happy,happy!

We see that although we just created the summer object, the __init__ () method was automatically invoked. The last line of the statement (Summer = Happybird ...) The object is created first, and then executes:
Copy Code code as follows:

summer.__init__ (More_words)

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

The nature of the object

We talked about a number of properties, but these are properties of the class. All objects belonging to this 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, this class of people. Sex is a character of a person, not all humans are male, or all 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 parameter of self. This parameter refers to an object of the class. We can modify the nature of an object by manipulating self. For example, using a class to create a new object, that is, the li_lei in the example below, then Li_lei is represented by self. By assigning value to Self.attribute, we add some properties to the Li_lei object, such as gender. Self is passed to each method. Within a method, you can query or modify the properties of an object by referencing Self.attribute.

This, in addition to the class attributes, adds to each object the nature of its features to describe a diverse world.

Copy Code code as follows:

Class Human (object):
def __init__ (self, Input_gender):
Self.gender = Input_gender
def printgender (self):
Print Self.gender

Li_lei = Human (' Male ') # here, ' Male ' is passed as a parameter to the __init__ variable of the Input_gender () method.
Print Li_lei.gender
Li_lei.printgender ()


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

Li_lei possessed the object nature gender. Gender is not a class attribute. Python, after establishing the object of Li_lei, uses the object nature of Li_lei.gender to store specific information that belongs to the object Li_lei.

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

Summarize

Calling class properties through self

__init__ (): Automatically executes when an object is created

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

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.