Practical use of objects and classes in basic python tutorials

  • 2020-04-02 14:03:29
  • OfStack

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

Additional information about the calling class

As mentioned in link: #, you must have the parameter self when you define a method. This parameter represents an object. The object has all the properties of the class, so we can call the class properties 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 called laugh. In the method show_laugh(), the value of the property is called through self.laugh.

Other methods can be called in the same way. Method show_laugh(), called in the method laugh_100th ().

Object allows you to modify class property values. But it is dangerous. Class attributes are Shared by all objects of the same class and its subclasses. Changes in class property values affect all objects.

__init__ () method

S) is a special method. Python has some special methods. Python gives them special treatment. The special method is characterized by two underscores before and after the name.

If you define the method of s/s () in the class, Python will call this method automatically when the object is created. 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!')

So here we have the Bird class, which is defined in the last video.

Print on screen:


We are happy birds.Happy,Happy!

We see that the method is called automatically, although we just created the summer object. The last line of the statement (summer = happyBird...) First create the object, then execute:

summer.__init__(more_words) 'Happy,Happy!' It was passed to __init__() The parameters of the more_words

Properties of objects

We talked about a lot of properties, but these properties are properties of the class. These properties are Shared by all objects belonging to the class. For example, birds have feathers and chickens can't fly.

In some cases, we define the properties of an object to record specific information about that object. For example, people. 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 an object of human beings, the gender is male; Han meimei is also an object of human beings, and her gender is female.

When defining a method for a class, you must pass a parameter to self. This parameter refers to an object of the class. We can manipulate self to change the properties of an object. For example, if you use a class to create a new object, li_lei in the following example, then li_lei is represented by self. We add some properties to the li_lei object by assigning a value to self.attribute, such as gender of male and female. Self is passed to each method. Within methods, you can query or modify the nature of an object by referring to self.attribute.

In this way, in addition to the class properties, each object adds its own characteristic properties to describe a diverse world.


class Human(object):
    def __init__(self, input_gender):
        self.gender = input_gender
    def printGender(self):
        print self.gender li_lei = Human('male') # Here, 'male' Passed as a parameter to __init__() methods input_gender The variable.
print li_lei.gender
li_lei.printGender()

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

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

The properties of the object can also be called by other methods, which are similar to calls to class properties, as in the printGender() method.

conclusion

Call the class property through self

S/s (): automatically executes while establishing the object

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


Related articles: