[英]Python: variables, inheritance, and default arguments
I think I have some misunderstandings of the usage of "class" and "inheritance' in Python. I'll simplify my question as the followings: 我想我对Python中“class”和“inheritance”的使用有一些误解。我将简化我的问题如下:
class A:
def __init__(self):
self.data = 100
class B(A):
def b(self):
print self.data
>>>B().b()
>>>100
OK, so far so good. 好的,到目前为止一切顺利。 However, if I create another class, something goes wrong, which is shown as the following:
但是,如果我创建另一个类,则会出现问题,如下所示:
class C(A):
def c(self, num=self.data):
print self.data
>>>C().c()
NameError: name 'self' is not defined
I want to set the default value of 'num' to self.data, which is '100'. 我想将'num'的默认值设置为self.data,即'100'。 Without 'class', it will be much simpler:
没有'class',它会更简单:
data = 100
def d(num = data):
print num
>>>d()
>>>100
I've already googled some articles, but still stuck in this problem... Thanks in advance! 我已经搜索了一些文章,但仍然坚持这个问题...提前谢谢!
When you do this: 当你这样做:
class C(A):
def c(self, num=self.data):
print self.data
you're doing something like: 你做的事情是这样的:
>>> def d(num, data=num):
... print(num, data)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined
And as you see the python compiler does not know what the second num
is. 正如您所看到的,python编译器不知道第二个
num
是什么。
But there's nothing stopping you from doing something like: 但没有什么可以阻止你做以下事情:
class C(A):
def c(self, num=None):
print num or self.data
or with an explicit None
check: 或者使用明确的
None
检查:
class C(A):
def c(self, num=None):
if num is None:
num = self.data
print num
This has nothing to do with inheritance. 这与继承无关。 You can try to do the same in the base class
A
and it would fail in the same way. 您可以尝试在基类
A
中执行相同操作,但它会以相同的方式失败。
For achieving what you want simply do: 为了达到你想要的目的,只需:
def c(self, num=None):
if num is None:
num = self.data
Function parameter defaults are evaluated when the function is defined, not when it is called. 函数参数默认值在定义函数时计算,而不是在调用函数时计算。 At definition time, there is no name
self
. 在定义时,没有名称
self
。
The best course is to use None as a default, and then use logic in the function to interpret what that means: 最好的方法是使用None作为默认值,然后在函数中使用逻辑来解释这意味着什么:
class C(A):
def c(self, num=None):
if num is None:
num = self.data
#.. the rest of the function ..
You are misunderstanding class methods. 你是对类方法的误解。 Class methods are implicitly passed self.
类方法是隐式传递给自己的。 Self does not exist
def c(self, num=HERE>>>>>self<<<<<HERE.data):
自我不存在
def c(self, num=HERE>>>>>self<<<<<HERE.data):
Why do you want to do what you are doing? 你为什么要做你正在做的事情?
class C(A):
def c(self,data=None):
if data == None:
print self.data
else
print data
The method of your class get arguments, you cannot pass self.something. 你的类获取参数的方法,你不能传递self.something。
As already mentioned, you can't use 'self' variable like that. 如前所述,你不能像这样使用'self'变量。 If you don't have to change the value of data later, you can do following:
如果以后不必更改数据值,可以执行以下操作:
class A:
data = 100
class C(A):
def me(self, num=A.data):
print num
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.