destroy your self????

Discussion in 'Python' started by KraftDiner, Oct 20, 2005.

  1. KraftDiner

    KraftDiner Guest

    if I create an object like...

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

    can obj set itself to none in some method of the class?
    KraftDiner, Oct 20, 2005
    #1
    1. Advertising

  2. KraftDiner

    Dave Brueck Guest

    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
    Dave Brueck, Oct 20, 2005
    #2
    1. Advertising

  3. KraftDiner

    Ron Adam Guest

    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
    Ron Adam, Oct 20, 2005
    #3
  4. KraftDiner

    KraftDiner Guest

    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 :)
    KraftDiner, Oct 20, 2005
    #4
  5. KraftDiner

    Dave Brueck Guest

    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.
    Dave Brueck, Oct 20, 2005
    #5
  6. KraftDiner

    Mike Meyer Guest

    "KraftDiner" <> 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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Oct 20, 2005
    #6
  7. KraftDiner

    James Guest

    Doesn't work for classes because self has no global reference.
    James, Oct 20, 2005
    #7
  8. KraftDiner

    Ron Adam Guest

    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
    Ron Adam, Oct 20, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    572
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    591
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    396
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    370
    falcon
    Jul 31, 2005
  5. Ook
    Replies:
    2
    Views:
    322
Loading...

Share This Page