In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what details should be paid attention to when defining attributes in python", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what details you need to pay attention to when defining attributes in python.
When defining attributes in object-oriented programming, we should pay attention to: some attributes are determined according to parameters when initializing the instance, while others can be deduced from other attributes. Be careful not to put such deductible attributes into the _ _ init__ function. The right thing to do is to define the inferred property as a function and disguise the function as an attribute through @ property.
As a case, define the circle class, which has three attributes: radius, perimeter and area.
Wrong case class Circle (): # define circular object def _ init__ (self, radius): # parameters are required for instance initialization-- radius self.radius = radius self.perimeter = 2*3.1415926535*self.radius self.area = 3.1415926535*self.radius**2a_Circle = Circle (5) print (radius of f 'circle is {a_Circle.radius}, its area is {a_Circle.area:.2f} Its perimeter is {a_Circle.perimeter:.2f}') a_Circle.radius = 10print (the radius of the f 'circle is {a_Circle.radius}, its area is {a_Circle.area:.2f}, its perimeter is {a_Circle.perimeter:.2f}') out: the radius of the circle is 5, its area is 78.54, the radius of the circle is 10, its area is 78.54, its perimeter is 31.42
In the above case, a circle class is defined, which has three attributes: radius, perimeter and area. There seems to be no problem with the definition, but the problem will be found after initialization.
When the radius of a circle is 5, there is no problem printing its radius, area, and perimeter, but when changing the radius value of the circle, the area and perimeter are not updated!
Correct case class Circle (): # define circular object def _ init__ (self, radius): # parameters required for instance initialization-- radius self.radius = radius @ propertydef perimeter (self): # return 2*3.1415926535*self.radius @ propertydef area (self): # radius of circle return 3.1415926535*self.radius**2a_Circle = Circle (5) print (radius of f 'circle is {a_Circle.radius} Its area is {a_Circle.area:.2f}, its perimeter is {a_Circle.perimeter:.2f}') a_Circle.radius = 10print (the radius of the circle is {a_Circle.radius}, its area is {a_Circle.area:.2f}, its perimeter is {a_Circle.perimeter:.2f}') out: the radius of the circle is 5, its area is 78.54,its perimeter is 31.42The radius of the circle is 10, and its area is 314.16. Its perimeter is 62.83.
In the above case, a circle class is defined, which has an attribute of radius, and the perimeter and area are functions that calculate the result based on the radius. The function can be disguised as an attribute by adding @ property before the function.
When the radius of the circle is 5, there is no problem printing its radius, area and perimeter; change when changing the radius value of the circle, the area and perimeter update synchronously!
At this point, I believe that everyone on the "python definition of attributes need to pay attention to what details" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.