By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

destroy your self????

P: n/a
if I create an object like...

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

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

Oct 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
KraftDiner wrote:
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
Oct 20 '05 #2

P: n/a
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 :)

Oct 20 '05 #3

P: n/a
KraftDiner wrote:
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.
Oct 20 '05 #4

P: n/a
KraftDiner wrote:
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?

def foo(): .... global foo
.... del foo
.... foo()
foo

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'foo' is not defined
Cheers,
Ron
Oct 20 '05 #5

P: n/a
"KraftDiner" <bo*******@yahoo.com> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 20 '05 #6

P: n/a
Doesn't work for classes because self has no global reference.

Oct 20 '05 #7

P: n/a
James wrote:
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
Oct 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.