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/03 Report--
This article mainly introduces what you need to pay attention to when using indentation in Python. What is introduced in this article is very detailed and has a certain reference value. Interested friends must finish reading it!
Recently, an object-oriented Python class was written, and an error was reported when it came to access restrictions (private attributes). The code is as follows, but the error is reported! (there is an error screenshot at the back)
Class Person (object): def run (self): print ("run") def eat (self,food): print ("eat" + food) def say (self): print ("My name is% s Magi I am% d years old"% (self.name,self.age)) # constructor The default initialization when creating an object is def _ _ init__ (self,name,age,height,weight,money): self.name = name self.age = age self.height = height self.weight = weight self.__money = money # is actually _ Person__money print ("Hello! I am% s and I am% d years old this year. The current deposit% f "% (self.name,self.age,self.__money)) # if you want the internal attribute not to be directly accessed externally, add _ _ before the attribute, it becomes a private attribute private self.__money = 100# Private attributes need to define get and set methods to access and assign def setMoney (self,money): if (money)
< 0): self.__money = 0 else: self.__money = money def getMoney(self): return self.__money person = Person("小明", 5, 120, 28,93.1) # 属性可直接被访问person.age = 10print(person.age) # 私有属性不可直接被访问或赋值,因为解释器把__money变成了_Person__money(可以用这个访问到私有属性的money,但是强烈建议不要),以下2行会报错# person.money = 10# print(person.__money) # 可以调用内部方法访问和赋值print(person.getMoney())person.setMoney(-10)print(person.getMoney()) Excuse me?!咋个就没有,那不上面大大摆着俩内部方法嘛! 昨天看着看着突然迸发了个小火星子,想起来缩进不对了,如图:Subtract one indent from the two methods, and even if it is out, it is the method of the class, juxtaposed with _ _ init__, and it will be correct.
Class Person (object): def run (self): print ("run") def eat (self,food): print ("eat" + food) def say (self): print ("My name is% s Magi I am% d years old"% (self.name,self.age)) # constructor The default initialization when creating an object is def _ _ init__ (self,name,age,height,weight,money): self.name = name self.age = age self.height = height self.weight = weight self.__money = money # is actually _ Person__money print ("Hello! I am% s and I am% d years old this year. Current deposit% f "% (self.name,self.age,self.__money)) # want the internal attribute not to be directly accessed externally, precede the attribute with _ _ It becomes a private attribute private self.__money = 100# Private attributes need to define get and set methods to access and assign def setMoney (self, money): if (money < 0): self.__money = 0 else: self.__money = money def getMoney (self): return self.__money person = Person ("Xiaoming", 5120 2881 93.1) # attributes can be accessed directly person.age = 10print (person.age) # Private attributes cannot be directly accessed or assigned, because the interpreter changes _ _ money to _ Person__money (you can use this to access the money of private properties But not strongly recommended), the following two lines will report an error # person.money = 1 destroy print (person.__money) # you can call the internal method to access and assign print (person.getMoney ()) person.setMoney (- 10) print (person.getMoney ())
The above is all the content of the article "what to pay attention to when using indentation in Python". Thank you for reading! Hope to share the content to help you, more related 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.