Python basic Tutorial: practical use of objects and Classes

Source: Internet
Author: User

Python basic Tutorial: practical use of objects and Classes

We are familiar with the basic concepts of objects and classes. We will further expand to make practical use of objects and classes.

Other information of the call class

As mentioned in the previous section, the self parameter must be included in the method definition. This parameter indicates an object. The object has all the properties of the class, so we can call the class attributes through self.

Copy codeThe Code is as follows:
Class Human (object ):
Laugh = 'hahahahaha'
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 the show_laugh () method, the value of this attribute is called through self. laugh.

You can also call other methods in the same way. The show_laugh () method is called in method laugh_100th.

You can modify the class property value through an object. But this is dangerous. Class attributes are shared by all objects of the same class and its subclass. Changes to class property values will affect all objects.

_ Init _ () method

_ Init _ () is a special method (special method ). Python has some special methods. Python treats them specially. The special method features two underscores (_) before and after the name.

If you define the _ init _ () method in the class, Python will automatically call this method when creating an object. This process is also called initialization.
Copy codeThe Code is as follows:
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. Its definition is described in the previous section.

Screen Printing:
Copy codeThe Code is as follows:
We are happy birds. Happy, Happy!

We can see that although we only created a summer object, the _ init _ () method is automatically called. The last line of statement (summer = happyBird...) first creates an object and then runs:
Copy codeThe Code is as follows:
Summer. _ init _ (more_words)

'Happy, Happy! 'The parameter more_words passed to _ init _ ()

Object nature

Many Attributes are mentioned, but these attributes are class attributes. All objects of this class share these attributes. For example, birds have feathers and chickens cannot fly.

In some cases, we define the nature of an object to record special information about the object. For example, the human class. Gender is a nature of a person. Not all humans are male or female. The value of this property varies with the object. Li Lei is a human object, with gender being male; Han Meimei being a human object, with gender being female.

When defining a class method, you must pass a self parameter. This parameter refers to an object of the class. We can modify the nature of an object by manipulating self. For example, if you use a class to create an object, that is, li_lei in the following example, li_lei is represented by self. We assign a value to self. attribute to add some properties to the li_lei object, such as gender and female. Self is passed to each method. In a method, you can reference self. attribute to query or modify the nature of an object.

In this way, in addition to class attributes, each object is added with its own special nature, which can describe the world of diversity.

Copy codeThe Code is 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 the parameter to the input_gender variable of the _ init _ () method.
Print li_lei.gender
Li_lei.printGender ()

During initialization, The input_gender parameter is assigned to the object property, that is, self. gender.

Li_lei has the object gender. Gender is not a class attribute. After establishing the li_lei object, Python uses the li_lei.gender object property to store the special information of the object li_lei.

The object can also be called by other methods. The Calling method is similar to the calling of class attributes, just as in the printGender () method.

Summary

Calling class attributes through self

_ Init _ (): automatically executed when an object is created

Differences between class attributes and object properties


How does one determine whether an object is an instance of a class in python?

You can use isinstance (s, myclass) to determine
If s is an instance of mycalss, True is returned; otherwise, False is returned.

What are Python classes, methods, objects, and instances? The concepts of methods, objects, and instances are rather vague,

Class is a set of functions. In this set, you define many functions. methods are actually the functions you define. In the following example, Class Plus is a Class. The two functions nested in this Class are called methods, but _ init _ is only used to initialize this Class, so it is not a method. The get_result function is a method.
For example:
Class Plus:
Def _ init _ (self, a, B)
Self. a =
Self. B = B
Def get_result (self)
Return self. a + self. B
In the above example, self is an object, which has two parameters: self. a. The other is self. b. The object is simply a variable with multiple attributes (or sub-variables. If the object is a general object, the instance is a specific object. The object is just a template with some attributes, and the instance is to fill in the data in this template. For example, if you write c1 = Plus (), c1 is an instance, and you can add c2 = Plus (), c2 is also an instance, however, they share common attributes and templates. The following example calls the method in the class:
Result1 = c1.get _ result () >>> 3. The output result is 3.
Result2 = c2.get _ result () >>> 5. The output result is 5.

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.