In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Python classes and objects, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Class
It is said that we should "find an object", but the first thing we see is a grammatical structure called "class". The class here is actually similar to the concept of "class" in our daily life. In our daily life, we put similar things into one category and give this category a name. For example, the common attribute of birds is that they have feathers and produce offspring by laying eggs. Any particular bird is based on the prototype of the bird.
Let's use Python to record the above ideas and describe birds:
Class Bird (object): feather = True reproduction = "egg"
Here, we define a class with the keyword class. The name of the class is Bird. There is a key word object in parentheses, which means "thing", that is, an individual. In computer language, we call individuals objects. Under a category, there can be multiple individuals. Birds can include the canary raised by the neighbor Lao Wang, the crow flying over the horizon, and a little yellow chicken raised at home.
Colons and indents indicate the code that belongs to this class. In the program blocks that belong to this category, we define two quantities, one to indicate that birds have feathers (feather) and the other to describe how birds reproduce (reproduction), which are called class attributes (attribute). The way we define birds is very rough. Birds are just "hairy things that can lay eggs". Biologists would probably shake their heads if they saw it, but after all, we took the first step in simulating the world.
In addition to distinguishing categories by data attributes, we sometimes distinguish them according to what such things can do. For example, birds can move. In this way, birds are distinguished from the types of houses. These actions will bring certain results, such as movement leading to a change of position. Some of these "behavior" properties are called method. In Python, methods are generally explained by defining functions within the class.
Class Bird (object): feather = True reproduction = "egg" def chirp (self, sound): print (sound)
We add a method attribute to birds, which is the method chirp () that represents the call of birds. The method chirp () looks a lot like a function. Its first argument is self, which refers to the object itself within the method, which I'll explain in more detail later. It is important to emphasize that whether this parameter is used or not, the first parameter of the method must be the self used to refer to the object itself. The remaining parameter sound is designed to meet our needs, which represents the content of birdsong. The method chirp () prints out the sound.
Object
We define classes, but like function definitions, this is just the process of building weapons. In order to use this sharp weapon, we need to go deep into the object level. By calling the class, we can create an object under the class. For example, I keep a chicken named summer. It is an object and belongs to birds. We use the previously defined birds to produce this object:
Summer = Bird ()
Create an object through this sentence and show that summer is an object that belongs to birds. Now, we can use the code that has been written in birds. Summer as an object will have the properties and methods of birds. The reference to the property is through the object. Property (object.attribute). For example:
Print (summer.reproduction) # print 'egg'
Using the above method, we get the reproduction mode of the class to which summer belongs.
In addition, we can call methods to ask summer to perform actions that birds allow. For example:
Summer.chirp ("jijiji") # print 'jijiji'
When we call the method, we pass only one parameter, the string "jijiji". This is the difference between methods and functions. Although we have to add this self parameter when defining a class's method, the self can only be inside the class definition, so there is no need to pass in data to the self when calling the method. By calling the chirp () method, my summer can be called.
Until now, the data that describes the object has been stored in the properties of the class. Class attributes describe the commonness of a class, such as birds with feathers. All objects belonging to this class share these properties. For example, summer is an object of birds, so summer also has feathers. Of course, we can refer to a class attribute through an object.
For all individuals under a class, there may be individual differences in some attributes. For example, my summer is yellow, but not all birds are yellow. For example, the category of people. Gender is a nature of a person, and not all human beings are male or female. The value of this property varies from object to object. Li Lei is an object of human beings, and his sex is male. Han Meimei is also an object of human beings, and her gender is female.
Therefore, in order to fully describe the individual, in addition to the common class attributes, we also need to describe the individual object attributes. In the class, we can manipulate the properties of the object through self. Now let's extend the Bird class:
Class Bird (object): def chirp (self, sound): print (sound) def set_color (self, color): self.color = colorsummer = Bird () summer.set_color ("yellow") print (summer.color) # print 'yellow'
In the method set_color (), we set the object's property color through the self parameter. Like class properties, we can pass through objects. Property to manipulate object properties. Because object properties depend on self, we have to manipulate class properties inside a method. Therefore, there is no way for an object property to assign an initial value directly below the class, like a class property.
However, Python still provides a way to initialize object properties. Python defines a series of special methods. Special methods are also known as magic methods (Magic Method). The method name of a special method is very special, with two underlines before and after it, such as init (), add (), dict (), and so on. Programmers can set special methods in the class definition. Python handles each particular method in a specific way. For the class's init () method, Python is called automatically each time an object is created. Therefore, we can initialize the object properties inside the init () method:
Class Bird (object): def _ init__ (self, sound): self.sound = soundprint ("my sound is:", sound) def chirp (self): print (self.sound) summer = Bird ("ji") summer.chirp () # print 'ji'
In the class definition above, we explain how this class is initialized through the init () method. The init () method is called whenever an object is created, such as when a summer object is created. It sets the object property sound. In the later chirp () method, you can call this object property through self. In addition to setting object properties, we can also add other instructions to init (). These instructions are executed when the object is created. When a class is called, the class can be followed by a list of parameters. The data you put here will be passed to the parameters of init (). With the init () method, we can initialize object properties when the object is created.
In addition to manipulating object properties, the self parameter has another function that allows us to call other methods of the same class within a method, such as:
Class Bird (object): def chirp (self, sound): print (sound) def chirp_repeat (self, sound, n): for i in range (n): self.chirp (sound) summer = Bird () summer.chirp_repeat ("ji", 10) # repeat print 'ji'10 times
In the method chirp_repeat (), we call another method in the class, chirp (), through self.
The above is all the content of the article "how to use Python classes and objects". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.