I have a class with an __init__
function.
我有一个带__init__函数的类。
How can I return an integer value from this function when an object is created?
如何在创建对象时从此函数返回整数值?
I wrote a program, where __init__
does command line parsing and I need to have some value set. Is it OK set it in global variable and use it in other member functions? If so how to do that? So far, I declared a variable outside class. and setting it one function doesn't reflect in other function ??
我写了一个程序,其中__init__执行命令行解析,我需要设置一些值。可以在全局变量中设置它并在其他成员函数中使用它吗?如果是这样怎么办?到目前为止,我在课外宣布了一个变量。并设置一个功能不反映在其他功能??
7 个解决方案
#1
73
__init__
is required to return None. You cannot (or at least shouldn't) return something else.
__init__必须返回None。你不能(或至少不应该)返回别的东西。
Try making whatever you want to return an instance variable (or function).
尝试制作任何想要返回实例变量(或函数)的内容。
>>> class Foo:
... def __init__(self):
... return 42
...
>>> foo = Foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() should return None
#2
88
Why would you want to do that?
你为什么想这么做?
If you want to return some other object when a class is called, then use the __new__()
method:
如果要在调用类时返回其他对象,请使用__new __()方法:
class MyClass(object):
def __init__(self):
print "never called in this case"
def __new__(cls):
return 42
obj = MyClass()
print obj
#3
37
From the documentation of __init__
:
从__init__的文档:
As a special constraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.
作为构造函数的特殊约束,不能返回任何值;这样做会导致在运行时引发TypeError。
As a proof, this code:
作为证明,这段代码:
class Foo(object):
def __init__(self):
return 2
f = Foo()
Gives this error:
给出了这个错误:
Traceback (most recent call last):
File "test_init.py", line 5, in <module>
f = Foo()
TypeError: __init__() should return None, not 'int'
#4
13
Sample Usage of the matter in question can be like:
相关问题的样本用法可以是:
class SampleObject(object)
def __new__(cls,Item)
if self.IsValid(Item):
return super(SampleObject, cls).__new__(cls)
else:
return None
def __init__(self,Item)
self.InitData(Item) #large amount of data and very complex calculations
...
ValidObjects=[]
for i in data:
Item=SampleObject(i)
if Item: # in case the i data is valid for the sample object
ValidObjects.Append(Item)
I do not have enough reputation so I can not write a comment, it is crazy! I wish I could post it as a comment to weronika
我没有足够的声誉所以我不能发表评论,这太疯狂了!我希望我可以发表它作为对weronika的评论
#5
12
The __init__
method, like other methods and functions returns None by default in the absence of a return statement, so you can write it like either of these:
在缺少return语句的情况下,__ init__方法与其他方法和函数一样,默认情况下返回None,因此您可以像以下任一方式一样编写它:
class Foo:
def __init__(self):
self.value=42
class Bar:
def __init__(self):
self.value=42
return None
But, of course, adding the return None
doesn't buy you anything.
但是,当然,添加回报“无”不会给你带来什么。
I'm not sure what you are after, but you might be interested in one of these:
我不确定你在追求什么,但你可能对以下其中一个感兴趣:
class Foo:
def __init__(self):
self.value=42
def __str__(self):
return str(self.value)
f=Foo()
print f.value
print f
prints:
打印:
42
42
#6
8
__init__
doesn't return anything and should always return None
.
__init__不返回任何内容,应始终返回None。
#7
2
Just wanted to add, you can return classes in __init__
只想添加,你可以在__init__中返回类
@property
def failureException(self):
class MyCustomException(AssertionError):
def __init__(self_, *args, **kwargs):
*** Your code here ***
return super().__init__(*args, **kwargs)
MyCustomException.__name__ = AssertionError.__name__
return MyCustomException
The above method helps you implement a specific action upon an Exception in your test
上述方法可帮助您在测试中对异常执行特定操作
#1
73
__init__
is required to return None. You cannot (or at least shouldn't) return something else.
__init__必须返回None。你不能(或至少不应该)返回别的东西。
Try making whatever you want to return an instance variable (or function).
尝试制作任何想要返回实例变量(或函数)的内容。
>>> class Foo:
... def __init__(self):
... return 42
...
>>> foo = Foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() should return None
#2
88
Why would you want to do that?
你为什么想这么做?
If you want to return some other object when a class is called, then use the __new__()
method:
如果要在调用类时返回其他对象,请使用__new __()方法:
class MyClass(object):
def __init__(self):
print "never called in this case"
def __new__(cls):
return 42
obj = MyClass()
print obj
#3
37
From the documentation of __init__
:
从__init__的文档:
As a special constraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.
作为构造函数的特殊约束,不能返回任何值;这样做会导致在运行时引发TypeError。
As a proof, this code:
作为证明,这段代码:
class Foo(object):
def __init__(self):
return 2
f = Foo()
Gives this error:
给出了这个错误:
Traceback (most recent call last):
File "test_init.py", line 5, in <module>
f = Foo()
TypeError: __init__() should return None, not 'int'
#4
13
Sample Usage of the matter in question can be like:
相关问题的样本用法可以是:
class SampleObject(object)
def __new__(cls,Item)
if self.IsValid(Item):
return super(SampleObject, cls).__new__(cls)
else:
return None
def __init__(self,Item)
self.InitData(Item) #large amount of data and very complex calculations
...
ValidObjects=[]
for i in data:
Item=SampleObject(i)
if Item: # in case the i data is valid for the sample object
ValidObjects.Append(Item)
I do not have enough reputation so I can not write a comment, it is crazy! I wish I could post it as a comment to weronika
我没有足够的声誉所以我不能发表评论,这太疯狂了!我希望我可以发表它作为对weronika的评论
#5
12
The __init__
method, like other methods and functions returns None by default in the absence of a return statement, so you can write it like either of these:
在缺少return语句的情况下,__ init__方法与其他方法和函数一样,默认情况下返回None,因此您可以像以下任一方式一样编写它:
class Foo:
def __init__(self):
self.value=42
class Bar:
def __init__(self):
self.value=42
return None
But, of course, adding the return None
doesn't buy you anything.
但是,当然,添加回报“无”不会给你带来什么。
I'm not sure what you are after, but you might be interested in one of these:
我不确定你在追求什么,但你可能对以下其中一个感兴趣:
class Foo:
def __init__(self):
self.value=42
def __str__(self):
return str(self.value)
f=Foo()
print f.value
print f
prints:
打印:
42
42
#6
8
__init__
doesn't return anything and should always return None
.
__init__不返回任何内容,应始终返回None。
#7
2
Just wanted to add, you can return classes in __init__
只想添加,你可以在__init__中返回类
@property
def failureException(self):
class MyCustomException(AssertionError):
def __init__(self_, *args, **kwargs):
*** Your code here ***
return super().__init__(*args, **kwargs)
MyCustomException.__name__ = AssertionError.__name__
return MyCustomException
The above method helps you implement a specific action upon an Exception in your test
上述方法可帮助您在测试中对异常执行特定操作