473,466 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Clean way to not get object back from instantiation attempt gonebad

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

Aug 15 '06 #1
11 1555
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('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

Aug 15 '06 #2
tobiah wrote:
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('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
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

Aug 16 '06 #3

tobiah wrote:
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('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?
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

Aug 16 '06 #4
tobiah <st@tobiah.orgwrites:
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?
I'm not sure I understand your goal, but it seems that you want to
handle two cases: one where Foo can be properly initialised, and one
where it can't -- an error condition.

That sounds like a job for exceptions.
>>class ConsumptionError(ValueError):
... """ Exception thrown from bad consumption """
...
>>class Foo(object):
... def __init__(self, left_thing, right_thing):
... if left_thing == 'grapes':
... raise ConsumptionError("Problem with consumption of fruit")
...
>>myfoo = Foo('zucchini', 'spam')
print myfoo
<__main__.Foo object at 0x401e446c>
>>myfoo = Foo('grapes', 'oranges')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in __init__
__main__.ConsumptionError: Problem with consumption of fruit
>>try:
... myfoo = Foo('grapes', 'oranges')
... except ConsumptionError, e:
... myfoo = None
...
>>print myfoo
None

--
\ Rommel: "Don't move, or I'll turn the key on this can of Spam!" |
`\ -- The Goon Show, _Rommel's Treasure_ |
_o__) |
Ben Finney

Aug 16 '06 #5
tobiah wrote:
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?
There's no way that the __init__() method can change the object of which
it's a method, since self is a variable local to the method (so changing
it won't change the object) and the method is required to return None.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Aug 16 '06 #6
Simon Forman wrote:
>
|>class f:
... def __init__(self):
... del self
Of course nothing happens. Args are local variables. 'self' is is a
vanilla arg of a vanilla function.
...
|>e = f()
|>e
<__main__.f instance at 0xb7dd91ec>
|>class f:
... def __init__(self):
... return None
Of course nothing different happens. There is always an implicit
"return None" when control falls off the end of a function. Making it
explicit changes nothing.
...
|>e = f()
|>e
<__main__.f instance at 0xb7dd934c>
The whole idea of "del self" or "return None" is not a goer. "self" is
a reference to the (mutable) newly created object. After __init__ has
finished mutating it, the constructor will return the object to the
constructor's caller.

The whole idea that None should be returned in the event of error is
.... well, let's just say it leaves me speechless.
But you could raise an exception and check for it:

|>class f:
... def __init__(self, flag=True):
... if not flag:
... raise
Please read the manual. A lone "raise" does *not* raise an anonymous
exception; it re-raises an exception that has just been trapped. If
there are none, it raises None, which causes (as documented) a
TypeError.
...
|>def f_factory(flag):
... try:
... e = f(flag)
... except:
Blanket exception catching is *never* a good idea. In this case the
exception being caught is an artifact of your use of the unadorned
"raise".

If you inserted here:
.... import sys
.... x, y = sys.exc_info()[:2]
.... print x, y
you would get:
exceptions.TypeError exceptions must be classes, instances, or strings
(deprecated), not NoneType
... e = None
... return e
...
|>foo = f_factory(True)
|>foo
<__main__.f instance at 0xb7dd944c>
|>foo = f_factory(False)
|>foo
|>print foo
None
HTH,
John

Aug 16 '06 #7
On Tue, 15 Aug 2006 16:04:12 -0700, tobiah wrote:
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?
I don't believe so.

Generally, in the event of an error, you should raise an exception:

class Foo():
def __init__(self, *fruits):
if "grapes" in fruits:
raise AllergyError("I'm allergic to grapes")
# process fruits

Then handle the exception:

try:
myfoo = Foo('grapes', 'oranges')
except AllergyError:
# recover gracefully
handle_failed_instance()
If you wish, you can wrap it in a function:

def safe_foo(*fruits):
try:
return Foo(*fruits)
except AllergyError:
return None

myfoo = safe_foo('grapes', 'oranges')

The disadvantage of this is now your code has to be sprinkled with a
million tests "if myfoo is not None: process(myfoo)".
Would the
__init__() just do del(self), or is there
a better way to think about this?

An alternative is to use a propagating "not a Foo" Foo object, like NaNs
and INFs in floating point.

class Foo():
def __init__(self, *fruits):
if "grapes" in fruits:
self._state = "failed" # NaNs use special bit patterns
else:
# process fruits
self._state = "okay"
def method(self, *args):
if self._state != "failed":
# process instance
else:
pass # do nothing
def __add__(self, other):
if self._state == "failed":
return self
elif other._state == "failed":
return other
else:
# process Foo addition
return something

Now you can call Foo methods regardless of whether the instance is valid
or not, errors will propagate cleanly, and you only need to check whether
the instance is valid at the very end of your code.

However, unless there is a clear advantage of doing this, you're creating
a fair bit of extra work for yourself.

--
Steven D'Aprano

Aug 16 '06 #8
John Machin wrote:
Simon Forman wrote:

|>class f:
... def __init__(self):
... del self

Of course nothing happens. Args are local variables. 'self' is is a
vanilla arg of a vanilla function.
I know.
...
|>e = f()
|>e
<__main__.f instance at 0xb7dd91ec>
|>class f:
... def __init__(self):
... return None

Of course nothing different happens. There is always an implicit
"return None" when control falls off the end of a function. Making it
explicit changes nothing.
I know
...
|>e = f()
|>e
<__main__.f instance at 0xb7dd934c>

The whole idea of "del self" or "return None" is not a goer. "self" is
a reference to the (mutable) newly created object. After __init__ has
finished mutating it, the constructor will return the object to the
constructor's caller.
I know that too.
The whole idea that None should be returned in the event of error is
... well, let's just say it leaves me speechless.
Tell me about it.
But you could raise an exception and check for it:

|>class f:
... def __init__(self, flag=True):
... if not flag:
... raise

Please read the manual. A lone "raise" does *not* raise an anonymous
exception; it re-raises an exception that has just been trapped. If
there are none, it raises None, which causes (as documented) a
TypeError.
I know
...
|>def f_factory(flag):
... try:
... e = f(flag)
... except:

Blanket exception catching is *never* a good idea. In this case the
exception being caught is an artifact of your use of the unadorned
"raise".
I know
If you inserted here:
... import sys
... x, y = sys.exc_info()[:2]
... print x, y
you would get:
exceptions.TypeError exceptions must be classes, instances, or strings
(deprecated), not NoneType
Yep.
... e = None
... return e
...
|>foo = f_factory(True)
|>foo
<__main__.f instance at 0xb7dd944c>
|>foo = f_factory(False)
|>foo
|>print foo
None

HTH,
John
The entire post was meant as a pedantic exercise illustrating what not
to do (and how easy it is to figure that out with an interactive
session,) and sketching how to raise and check for an error instead.

In the three seconds I spent thinking about it, I couldn't decide what
exception to raise, so I used the wicked bare raise and except. I'm
certainly not advocating their use, but perhaps I should've made that
plain in a post intended to be "educational".

In any event, with your comments it's out there now, and the
educational value of this entire thread has been improved. Thanks
John.

Peace,
~Simon

Aug 16 '06 #9

Simon Forman wrote:
The entire post was meant as a pedantic exercise illustrating what not
to do (and how easy it is to figure that out with an interactive
session,) and sketching how to raise and check for an error instead.
Lessons usually start with the teacher asserting authority and stating
the objective of the lesson, especially if the main content is a list
of things the students should *not* do. Otherwise, casual observers can
only apply duck-typing :-)
Peace,
et pax vobiscum ...

Aug 16 '06 #10
tobiah wrote:
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?
There is a way, of course, that results in myfoo
being None in case of an error, but it is not a one-liner and
I'd not recommend it.
If something goes wrong, raising an exception is the best thing to do.

Georg
Aug 18 '06 #11

tobiah wrote:
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
As others have said, just raise an exception. You can hide
instantiation inside a factory function to simulate the behaviour
you're specifically talking about:

class Foo:
def __init__(self, *args):
for arg in args:
if is_fruit(arg):
raise RuntimeError("I don't like fruit")
def FooFactory(*args):
try:
return Foo(*args)
except RuntimeError:
return None

-Grant

Aug 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: robc | last post by:
hi, I wrote a container template similar to that below, assuming that as long as I never called destroy() for a non-pointer instantiation the compiler would not attempt to generate that method...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
2
by: David | last post by:
Assuming of course that the instantiation statement is 100% ok. As a matter of fact, that very instantiation passes successfuly on VC++ 7.1 (and Borland) but since I was suspicious that it was OK...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
8
by: plenty900 | last post by:
Hello, I'm trying to build a large project, and I've found that when something doesn't compile properly, and I attempt to re-build it, Visual C++ Express doesn't make a new attempt. So then I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.