destroy your self????

K

KraftDiner

if I create an object like...

obj = None
....
obj = anObject()

can obj set itself to none in some method of the class?
 
D

Dave Brueck

KraftDiner said:
if I create an object like...

obj = None
...
obj = anObject()

can obj set itself to none in some method of the class?

No - Python doesn't work that way.

What are you trying to accomplish? There's probably a way to do what you need to
do, but this isn't it.

-Dave
 
R

Ron Adam

KraftDiner said:
if I create an object like...

obj = None
...
obj = anObject()

can obj set itself to none in some method of the class?


Do you mean like this?

.... global foo
.... del foo
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'foo' is not defined


Cheers,
Ron
 
K

KraftDiner

Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None :)
 
D

Dave Brueck

KraftDiner said:
Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None :)

In Python, variables don't "hold" values - variables are just named references
to objects:

x = 5 # x refers to the 5 integer object
x = 'hi' # now x refers to the string object

By reassigning a name to something else, that's all you're doing - ever other
reference to that object will still be intact.

If you were able to flag the degenerate case, code that uses the object would
have to check for it, right? That being the case, the same code could just as
easily test a member function.

Better yet, if you can catch this case at the point when the object is created,
just throw an exception or at least perform the test at that point only.
 
M

Mike Meyer

KraftDiner said:
Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None :)

You're running headlong into the fact that Python's variables are just
ways to reference objects - "names" if you will - and not objects
themselves. Whe you write "foo = some_obj", all you're doing is making
tha name "foo" refer to some_obj instead of to whatever it was
referring to before. Neither some_obj nor the object previously known
as "foo" are changed in anyway.

To make a change in the object, you have to make a change in the
object proper, not just a name that refers to it. Adding a flag that
clients can test to see if the object is still valid would do
it. Changing the operations that clients do on the object so they
raise exceptions will also work, and might be considered more
Pythonic.

<mike
 
R

Ron Adam

James said:
Doesn't work for classes because self has no global reference.

True. To make it work one would need to track instances and names and
do comparisons... and so on. So it's not worth it. ;-)

Cheers,
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top