Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Cancel instance create

0 views
Skip to first unread message

Fredrik Lundh

unread,
Sep 6, 2008, 7:11:04 AM9/6/08
to pytho...@python.org
Aigars Aigars wrote:

> I want MyClass to perform some tests and if them fail, I do not want
> instance to be created.
>
> But in code I wrote instance is created and also has parameters, that it
> should not have in case of tests failure.
>
> Is there a way to perform tests in MyClass.__init__ and set instance to
> None without any parameters?

if you want construction to fail, raise an exception.

...

def __init__(self):
self.param = "spam"
Test = False
if Test: # please don't use explicit tests for truth
print "Creating instance..."
else:
raise ValueError("some condition failed")

(pick an exception class that matches the actual error, or create your
own class if necessary)

</F>

Mohamed Yousef

unread,
Sep 6, 2008, 8:04:27 AM9/6/08
to Fredrik Lundh, pytho...@python.org
ًWhat about no Constructor , and a custom instancing function that can
return either None or the instance wanted

Diez B. Roggisch

unread,
Sep 6, 2008, 8:23:23 AM9/6/08
to
Mohamed Yousef schrieb:

> ًWhat about no Constructor , and a custom instancing function that can
> return either None or the instance wanted

That doesn't solve the underlying problem - the instance is created.
Just because it wasn't *returned*, doesn't mean it isn't there.

Diez

Terry Reedy

unread,
Sep 6, 2008, 1:35:23 PM9/6/08
to pytho...@python.org

Fredrik Lundh wrote:
> Aigars Aigars wrote:
>
>> I want MyClass to perform some tests and if them fail, I do not want
>> instance to be created.

If you do not want the instance created at all, you would have to write
a custom .__new__ method, but that is tricky, something I would have to
look up how to do, and most likely not needed. Fredrik's solution below
is much easier and the one I would use if at all possible.

>> But in code I wrote instance is created and also has parameters, that
>> it should not have in case of tests failure.
>>
>> Is there a way to perform tests in MyClass.__init__ and set instance
>> to None without any parameters?
>
> if you want construction to fail, raise an exception.
>
> ...
>
> def __init__(self):

At this point, you have an instance of your class bound to local name
'self'. Usually, its only individual attributes are (in 3.0, anyway, as
far as I can tell) .__class__ and an empty .__dict__. Of course, it
inherits class and superclass attributes, but it is otherwise blank.
(The main exception would be if you were inheriting from an immutable
class that set attributes in .__new__, but then you would not be writing
.__init__.)

> self.param = "spam"
> Test = False
> if Test: # please don't use explicit tests for truth
> print "Creating instance..."
> else:
> raise ValueError("some condition failed")
>
> (pick an exception class that matches the actual error, or create your
> own class if necessary)

Once the exception gets disposed of (if not before), the blank instance
gets unbound from 'self' and since it does not get bound to anything
else, it becomes eligible for garbage collection.

tjr

Fuzzyman

unread,
Sep 9, 2008, 6:59:35 PM9/9/08
to
On Sep 6, 1:23 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> Mohamed Yousef schrieb:
>
> > ðWhat about no Constructor , and a custom instancing function that can


def __new__(cls, *args, **kwargs):
if some_condition:
return None
return object.__new__(cls)

Michael Foord
--
http://www.ironpythoninaction.com/

0 new messages