[英]Returning class to itself in python
Very simple question here, but I am new to python as an object oriented programming language.这里的问题很简单,但我对 python 作为面向对象的编程语言不熟悉。 I am trying to write a class.我正在尝试编写一个类。 Imagine it is organized as follows:想象一下,它的组织方式如下:
class myClass:
def __init__(self,a,b,runit=True):
self.a = a
self.b = b
if runit:
self.run_func()
def run_func(self):
self.c = self.a*self.b
return
So as you can see the class is initialized with just a
and b
.如您所见,该类仅使用a
和b
初始化。 It defaults to initialize c
from those arguments, but it need not.它默认从这些参数初始化c
,但它不需要。 Now let me illustrate three use cases that I think should behave the same, but are not:现在让我说明三个我认为应该表现相同但不是:
# Use 1
test = myClass(5,2)
print(test.c)
# Use 2
test = myClass(5,2,runit=False)
test.run_func()
print(test.c)
# Use 3
test = myClass(5,2,runit=False).run_func()
print(test.c)
This returns the following:这将返回以下内容:
10
10
Traceback (most recent call last):
File "<ipython-input-37-cb854baa3a0c>", line 23, in <module>
print(test.c)
AttributeError: 'NoneType' object has no attribute 'c'
Why can the instantiated class not be operated on immediately and pipe this result to test
in one step?为什么不能立即对实例化的类进行操作,并通过管道将这个结果一步一步地进行test
? In my head, (1) and (2) are the same set of operations except one is broken into two steps and the other is done in one line.在我的脑海中,(1)和(2)是一组相同的操作,只是一个被分成两个步骤,另一个在一行中完成。
And, more importantly, how can I fix this by editing my class to behave in the expected manner?而且,更重要的是,我如何通过编辑我的类以按预期的方式行事来解决这个问题?
run_func
should return self
: run_func
应该返回self
:
def run_func(self):
self.c = self.a*self.b
return self
stating return
without a value following it, is the same as writing return None
.声明return
后面没有值,与写return None
相同。 You get this exception because None
has no attribute named c
.您会收到此异常,因为None
没有名为c
属性。
In the 3rd case, test
is assigned the return value of run_func()
.在第三种情况下, test
被分配了run_func()
的返回值。 It has a bare return
which returns None
, so test = None
.它有一个返回None
的裸return
,所以test = None
。
at # Use 3
myClass(5,2,runit=False).run_func()
returns None
at # Use 3
myClass(5,2,runit=False).run_func()
返回None
to fix you could return self
:修复你可以返回self
:
def run_func(self):
self.c = self.a*self.b
return self
test = myClass(5,2,runit=False).run_func()
print(test.c)
output:输出:
10
or you should not set your flag runit
to False
and use:或者您不应该将标志runit
设置为False
并使用:
class myClass:
def __init__(self,a,b,runit=True):
self.a = a
self.b = b
if runit:
self.run_func()
def run_func(self):
self.c = self.a*self.b
test = myClass(5,2)
print(test.c)
output:输出:
10
Lets look at the method call in question:让我们看看有问题的方法调用:
test = myClass(5,2,runit=False).run_func()
Let's break that down.让我们分解一下。 First, you construct an instance of myClass
and then you call run_func
on that instance.首先,您构造一个myClass
的实例,然后在该实例上调用run_func
。 It's the return value of run_func
that you assign to test and since run_func
doesn't return anything test
is None
resulting in your error.这是您分配给 test 的run_func
的返回值,并且由于run_func
不返回任何内容test
is None
导致您的错误。
As another way to see this, try the following:作为查看这一点的另一种方式,请尝试以下操作:
class myClass:
def __init__(self,a,b,runit=True):
self.a = a
self.b = b
if runit:
self.run_func()
def run_func(self):
self.c = self.a*self.b
return 11
test = myClass(5,2,runit=False).run_func()
print(test) # will print 11
In case 3情况 3
test = myClass(5,2,runit=False).run_func()
print(test.c)
test
is not the object, but the return of the run_func(), which is null and indeed has no c attribute test
不是对象,而是run_func()的返回,为null,确实没有c属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.