为什么我不能从下面 Python 中的方法之一访问 class 变量?
Why cant I access class variable from one of it's method as below in Python?
我制作了一个名为 'employee' 的 class,如下所示。我可以直接通过 class 本身访问 class 变量 'company' 但不能使用 'getCompany()' 方法访问它。我的代码有什么问题?由于我是 OOP 概念的新手,请 详细但逐步地阐述这些概念 。
<!-- language: lang-python -->
>>> class employee:
company = 'ABC Corporation'
def getCompany():
return company
>>> employee.company #####this does as expected
'ABC Corporation'
>>> employee.getCompany() #####what is wrong with this thing???
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
employee.getCompany()
File "<pyshell#13>", line 4, in getCompany
return company
NameError: name 'company' is not defined #####says it is not defined
我是 OOP 概念的新手。
解释器正在查找同名的局部变量,但该变量不存在。您还应该将 self
添加到参数中,以便您拥有适当的实例方法。如果您想要静态方法而不是实例方法,则需要添加 @staticmethod
装饰器。最后,使用 class 名称来引用 class 变量。
>>> class employee:
... company = 'ABC Corporation'
... def getCompany(self=None):
... return employee.company
...
>>> employee.company
'ABC Corporation'
>>> employee.getCompany()
'ABC Corporation'
>>> e = employee()
>>> e.getCompany()
'ABC Corporation'
>>> e.company
'ABC Corporation'
In [1]: class employee:
...: company = "ABC Corporation"
...: def getCompany(self):
...: return self.company
...:
In [2]: employee.company
Out[2]: 'ABC Corporation'
In [3]: employee().getCompany()
Out[3]: 'ABC Corporation'
In [4]: class employee:
...: company = "ABC Corporation"
...:
...: @classmethod
...: def getCompany(self):
...: return self.company
...:
In [5]: employee.company
Out[5]: 'ABC Corporation'
In [6]: employee.getCompany()
Out[6]: 'ABC Corporation'
问题 Static class variables in Python 有更多详细信息
我制作了一个名为 'employee' 的 class,如下所示。我可以直接通过 class 本身访问 class 变量 'company' 但不能使用 'getCompany()' 方法访问它。我的代码有什么问题?由于我是 OOP 概念的新手,请 详细但逐步地阐述这些概念 。
<!-- language: lang-python -->
>>> class employee:
company = 'ABC Corporation'
def getCompany():
return company
>>> employee.company #####this does as expected
'ABC Corporation'
>>> employee.getCompany() #####what is wrong with this thing???
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
employee.getCompany()
File "<pyshell#13>", line 4, in getCompany
return company
NameError: name 'company' is not defined #####says it is not defined
我是 OOP 概念的新手。
解释器正在查找同名的局部变量,但该变量不存在。您还应该将 self
添加到参数中,以便您拥有适当的实例方法。如果您想要静态方法而不是实例方法,则需要添加 @staticmethod
装饰器。最后,使用 class 名称来引用 class 变量。
>>> class employee:
... company = 'ABC Corporation'
... def getCompany(self=None):
... return employee.company
...
>>> employee.company
'ABC Corporation'
>>> employee.getCompany()
'ABC Corporation'
>>> e = employee()
>>> e.getCompany()
'ABC Corporation'
>>> e.company
'ABC Corporation'
In [1]: class employee:
...: company = "ABC Corporation"
...: def getCompany(self):
...: return self.company
...:
In [2]: employee.company
Out[2]: 'ABC Corporation'
In [3]: employee().getCompany()
Out[3]: 'ABC Corporation'
In [4]: class employee:
...: company = "ABC Corporation"
...:
...: @classmethod
...: def getCompany(self):
...: return self.company
...:
In [5]: employee.company
Out[5]: 'ABC Corporation'
In [6]: employee.getCompany()
Out[6]: 'ABC Corporation'
问题 Static class variables in Python 有更多详细信息