Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python对象属性命名_Python 3.x_Attributes - Fatal编程技术网

Python 3.x Python对象属性命名

Python 3.x Python对象属性命名,python-3.x,attributes,Python 3.x,Attributes,关于Python类/对象属性,我有一个问题要问。看看这段代码: class Car: def __init__(self, year, model): self.__year_model = year self.__make = model self.__speed = 0 def Accelerate(self): self.__speed += 5 def Brake(self): se

关于Python类/对象属性,我有一个问题要问。看看这段代码:

class Car:
    def __init__(self, year, model):
        self.__year_model = year
        self.__make = model
        self.__speed = 0 
    def Accelerate(self):
        self.__speed += 5
    def Brake(self):
        self.__speed -= 5
    def get_speed(self):
        print(f"The current speed of this car is {self.__speed} miles")

myCar = Car(2019, 'toyota'.title())
print(f"The name of the car is {myCar.__make} and the manufacturing date is {myCar.__year_model} ")

myCar.Accelerate()
myCar.get_speed()
myCar.Brake()
myCar.get_speed()

从Python解释器运行时,我得到一个AttributeError,如下所示:

Traceback (most recent call last):
  File "c:\Users\Gin-san\Untitled-2.py", line 14, in <module>
    print(f"The name of the car is {myCar.__make} and the manufacturing date is {myCar.__year_model}")
AttributeError: 'Car' object has no attribute '__make'
我可以得到为什么代码没有在第一个实例中运行,而是在第二个实例中运行的原因吗?
谢谢。

这是因为python中的双下划线表示您在类中声明了一个私有变量。无法从定义的类外部访问此私有变量


您可以在这里获得更多信息:

在联机阅读和Python 3文档之后。我发现Python解释器通过将classname
\u classname\uu var
放在变量名前面,去掉了任何带有两个前导下划线的变量名。这是为了防止以后扩展类时发生任何冲突

因此,第一个代码应该是:

class Car:
    def __init__(self, year, model):
        self.__year_model = year
        self.__make = model
        self.__speed = 0 
    def Accelerate(self):
        self.__speed += 5
    def Brake(self):
        self.__speed -= 5
    def get_speed(self):
        print(f"The current speed of this car is {self.__speed} miles")

myCar = Car(2019, 'toyota'.title())
print(f"The name of the car is {myCar._Car__make} and the manufacturing date is {myCar._Car__year_model}")

myCar.Accelerate()
myCar.get_speed()
myCar.Brake()
myCar.get_speed()
请注意,调用时,
\u year\u model
\u make
已更改为
\u Car\u year\u model
\u Car\u make
,以便访问

The name of the car is Toyota and the manufacturing date is 2019
The current speed of this car is 5 miles
The current speed of this car is 0 miles
class Car:
    def __init__(self, year, model):
        self.__year_model = year
        self.__make = model
        self.__speed = 0 
    def Accelerate(self):
        self.__speed += 5
    def Brake(self):
        self.__speed -= 5
    def get_speed(self):
        print(f"The current speed of this car is {self.__speed} miles")

myCar = Car(2019, 'toyota'.title())
print(f"The name of the car is {myCar._Car__make} and the manufacturing date is {myCar._Car__year_model}")

myCar.Accelerate()
myCar.get_speed()
myCar.Brake()
myCar.get_speed()