干净的方式不从实例化尝试回来的对象回来 [英] Clean way to not get object back from instantiation attempt gonebad
问题描述
假设我这样做:
myfoo = Foo(''grape'',''oranges'')
并且在__init __()中Foo,水果的消费还有
a真正的问题。
有没有一种干净的方法来确保myfoo
将是无呼叫?请问
__init __()只做del(自我),还是
a更好的方式来考虑这个?
谢谢,
Toby
-
通过 http://www.teranews.com
Suppose I do:
myfoo = Foo(''grapes'', ''oranges'')
And in the __init__() of Foo, there is
a real problem with the consumption of fruit.
Is there a clean way to ensure that myfoo
will be None after the call? Would the
__init__() just do del(self), or is there
a better way to think about this?
Thanks,
Toby
--
Posted via a free Usenet account from http://www.teranews.com
推荐答案
我应该更清楚地知道Foo是一个类:
class Foo:
def __init __(self,* args):
arg中arg的b $ b:
if is_fruit(arg):
del(self)
tobiah写道:
I should have made it more clear that Foo is a class:
class Foo:
def __init__(self, *args):
for arg in args:
if is_fruit(arg):
del(self)
tobiah wrote:
假设我这样做:
myfoo = Foo(''grape '','''oranges'')
在Foo的__init __()中,水果的消费确实存在问题。
a真正的问题。 />
有没有一种干净的方法可以确保myfoo
在通话后为无?请问
__init __()只做del(自我),还是
a更好的方式来考虑这个?
谢谢,
Toby
Suppose I do:
myfoo = Foo(''grapes'', ''oranges'')
And in the __init__() of Foo, there is
a real problem with the consumption of fruit.
Is there a clean way to ensure that myfoo
will be None after the call? Would the
__init__() just do del(self), or is there
a better way to think about this?
Thanks,
Toby
--
Posted via a free Usenet account from http://www.teranews.com
tobiah写道:
tobiah wrote:
我应该更清楚地知道Foo是一个类:
Foo:
def __init __(self,* args):
arg中arg的
:
if is_fruit(arg):
del(self)
tobiah写道:
I should have made it more clear that Foo is a class:
class Foo:
def __init__(self, *args):
for arg in args:
if is_fruit(arg):
del(self)
tobiah wrote:
假设我做:
myfoo = Foo('''葡萄'',''橘子'')
在Foo的__init __()中,有< br $>
a水果消费的实际问题。
有没有一种干净的方法可以确保myfoo
w电话后生病了吗?请问
__init __()只做del(自我),还是
a更好的方式来考虑这个?
谢谢,
托比
Suppose I do:
myfoo = Foo(''grapes'', ''oranges'')
And in the __init__() of Foo, there is
a real problem with the consumption of fruit.
Is there a clean way to ensure that myfoo
will be None after the call? Would the
__init__() just do del(self), or is there
a better way to think about this?
Thanks,
Toby
我认为没有办法做到这一点(但是我可能错了):
|>类f:
.... def __init __(自我):
.... del self
....
|> e = f()
|> e
< __ main __。f实例位于0xb7dd91ec>
|>类f:
.... def __init __(自我):
....返回无
....
|> e = f()
|> e
< __ main __。f实例位于0xb7dd934c>
但你可以提出异常并检查它:
|>类f:
.... def __init __(self,flag = True):
....如果没有标记:
....提高
....
|> def f_factory(flag):
....尝试:
.... e = f(旗帜)
....除了:
.... e =无
....返回e
....
|> foo = f_factory(真)
|> foo
< __ main __。f实例位于0xb7dd944c>
|> foo = f_factory(False)
|> foo
|>打印foo
无
可能有办法使用__new __(),但我不知道是什么或如何。
此外,del是一种陈述,而不是一种功能。你不需要使用
()的。
HTH,
~西蒙
I don''t think there is a way to do this (but I could be wrong):
|>class f:
.... def __init__(self):
.... del self
....
|>e = f()
|>e
<__main__.f instance at 0xb7dd91ec>
|>class f:
.... def __init__(self):
.... return None
....
|>e = f()
|>e
<__main__.f instance at 0xb7dd934c>
But you could raise an exception and check for it:
|>class f:
.... def __init__(self, flag=True):
.... if not flag:
.... raise
....
|>def f_factory(flag):
.... try:
.... e = f(flag)
.... except:
.... e = None
.... return e
....
|>foo = f_factory(True)
|>foo
<__main__.f instance at 0xb7dd944c>
|>foo = f_factory(False)
|>foo
|>print foo
None
There might be a way using __new__(), but I don''t know what or how.
Also, "del" is a statement, not a function. You don''t need to use
()''s.
HTH,
~Simon
tobiah写道:
tobiah wrote:
我应该更明确地说Foo是一个类:
类Foo:
def __init __(self,* args):
for arg args:
if is_fruit(arg):
del(self)
tobiah写道:
I should have made it more clear that Foo is a class:
class Foo:
def __init__(self, *args):
for arg in args:
if is_fruit(arg):
del(self)
tobiah wrote:
假设我这样做:
Suppose I do:
- * myfoo = Foo(''grape'' ,''oranges'')
-* myfoo = Foo(''grapes'', ''oranges'')
在Foo的__init __()中,有
a水果消费的实际问题。
有没有一种干净的方法可以确保myfoo
在通话结束后没有?请问
__init __()只做del(自我),还是
更好的方式来考虑这个?
And in the __init__() of Foo, there is
a real problem with the consumption of fruit.
Is there a clean way to ensure that myfoo
will be None after the call? Would the
__init__() just do del(self), or is there
a better way to think about this?
是的。提出异常,详细说明问题所在 -
arg?什么(多个可能出现的问题)是错的?
如果self.is_fruit(arg):
提高FooError(我不喜欢吃水果:%r"%arg)
如果self.some_other_problem(arg):
提高FooError(其他一些问题:%r%arg)
HTH,
John
Yes. Raise an exception, with details of what the problem is -- which
arg? what (out of multiple possible problems) is wrong with it?
if self.is_fruit(arg):
raise FooError("I don''t eat fruit: %r" % arg)
if self.some_other_problem(arg):
raise FooError("Some other problem: %r" % arg)
HTH,
John
这篇关于干净的方式不从实例化尝试回来的对象回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!