Can you set a class instance's attributes to zero by setting the instance to zero?

Discussion in 'Python' started by Gerard Flanagan, Nov 19, 2005.

  1. Hello

    If I have the Vector class below, is there a means by which I can have
    the following behaviour


    >>>A = Vector(1, 2)
    >>>print A

    (1, 2)
    >>>A = 0
    >>>print A

    (0, 0)

    If there is such a means, will it still work with the __slots__
    attribution uncommented?

    Thanks

    class Vector(object):

    #__slots__ = ('x', 'y')

    def __init__(self, a=0, b=0 ):
    self.x = a
    self.y = b

    def __repr__(self):
    return '(%s, %s)' % (self.x, self.y)
     
    Gerard Flanagan, Nov 19, 2005
    #1
    1. Advertising

  2. Re: Can you set a class instance's attributes to zero by settingthe instance to zero?

    Gerard Flanagan wrote:
    > Hello
    >
    > If I have the Vector class below, is there a means by which I can have
    > the following behaviour
    >
    >
    >
    >>>>A = Vector(1, 2)
    >>>>print A

    >
    > (1, 2)
    >
    >>>>A = 0
    >>>>print A

    >
    > (0, 0)
    >
    > If there is such a means, will it still work with the __slots__
    > attribution uncommented?


    No, you can't. The reason is that python doesn't have an assignment
    operator as e.g. C++ has. The "=" just binds the name A to some object -
    without the object knowing it.

    What's wrong with

    class A:
    def clear():
    pass
    ...

    A.clear()

    ? Alternatively, you could try and abuse one of the seldom used in-place
    operator like __ior__:

    A |= 0

    But I wouldn't recommend that.

    Regards,

    Diez
     
    Diez B. Roggisch, Nov 19, 2005
    #2
    1. Advertising

  3. Re: Can you set a class instance's attributes to zero by settingtheinstance to zero?

    Gerard Flanagan wrote:

    > If I have the Vector class below, is there a means by which I can have
    > the following behaviour
    >
    >>>>A = Vector(1, 2)
    >>>>print A

    > (1, 2)


    >>>>A = 0


    that operation rebinds A; it doesn't affect the Vector instance in any way.

    more here:

    http://effbot.org/zone/python-objects.htm

    </F>
     
    Fredrik Lundh, Nov 19, 2005
    #3
  4. Re: Can you set a class instance's attributes to zero by settingthe instance to zero?

    On 19 Nov 2005 05:29:07 -0800
    "Gerard Flanagan" <> wrote:
    > If I have the Vector class below, is there a means by
    > which I can have the following behaviour
    >
    >
    > >>>A = Vector(1, 2)
    > >>>print A

    > (1, 2)
    > >>>A = 0
    > >>>print A

    > (0, 0)


    As has already been mentioned, "A = 0" rebinds the name "A"
    to the object "0" so there's no way it can mutate the object
    A.

    However, you could create an object "Origin" to use as a
    vector zero:

    Origin = Vector(0,0)

    Or if you want to be shorter, more poetic, and make future
    maintainers curse you, you can call it "O":

    O = Vector(0,0)

    ;-)

    --
    Terry Hancock ()
    Anansi Spaceworks http://www.AnansiSpaceworks.com
     
    Terry Hancock, Nov 19, 2005
    #4
    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. Zhiqiang Ye
    Replies:
    53
    Views:
    10,417
    Dan Pop
    Jun 28, 2004
  2. Robert Oschler
    Replies:
    3
    Views:
    501
    Christopher T King
    Jul 27, 2004
  3. mrdylan
    Replies:
    3
    Views:
    327
    Steven D'Aprano
    Apr 12, 2006
  4. Sandra-24
    Replies:
    18
    Views:
    1,942
    Steve Holden
    Apr 29, 2006
  5. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    414
    Bruno Desthuilliers
    Jun 20, 2007
Loading...

Share This Page