Home >Backend Development >Python Tutorial >Object-oriented in-depth

Object-oriented in-depth

高洛峰
高洛峰Original
2016-10-19 11:14:421180browse

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

Other information about the calling class

As mentioned in the previous lecture, when defining a method, the self parameter must be included. This parameter represents an object. The object has all the properties of the class, so we can call the class attributes through self.

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()

There is a class attribute laugh here. In the method show_laugh(), the value of this attribute is called through self.laugh.

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

Class attribute values ​​can be modified through objects. But this is dangerous. Class attributes are shared by all objects of the same class and its subclasses. Changes to class attribute values ​​affect all objects.

__init__() method

__init__() is a special method. Python has some special methods. Python will treat them specially. Special methods are characterized by two underscores before and after the name.

If you define the __init__() method in your class, Python will automatically call this method when creating an 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, its definition is shown in the previous lecture.

Print on the screen:

We are happy birds.Happy,Happy!

We see that although we just created the summer object, the __init__() method is automatically called. The last line of statements (summer = happyBird...) first creates the object and then executes:

summer.__init__(more_words)

'Happy, Happy!' is passed to the parameter more_words

object of __init__() Properties of

We talked about many attributes, but these attributes are attributes of classes. All objects belonging to this class share these properties. For example, birds all have feathers and chickens cannot fly.

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

When defining a class method, you must pass a self parameter. This parameter refers to an object of the class. We can modify the properties of an object by manipulating self. For example, if you use a class to create a new object, that is, li_lei in the following example, then li_lei is represented by self. By assigning a value to self.attribute, we add some properties to the object li_lei, such as male and female gender. self will be passed to each method. Inside the method, you can query or modify the properties of the object by referencing self.attribute.

In this way, in addition to the class attributes, each object is given its own unique properties, so that it can describe a variety of worlds.

class Human(object):
    def __init__(self, input_gender):
        self.gender = input_gender
    def printGender(self):
        print self.gender
li_lei = Human('male') # 这里,'male'作为参数传递给__init__()方法的input_gender变量。
print li_lei.gender
li_lei.printGender()

In the initialization, assign the parameter input_gender to the property of the object, that is, self.gender.

li_lei has the object property gender. Gender is not a class attribute. After Python creates the object li_lei, it uses the object property li_lei.gender to specifically store the unique information belonging to the object li_lei.

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

Summary

Calling class attributes through self

__init__(): automatically executed when creating an object

The difference between class attributes and the properties of objects


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn