SOS - property question

Discussion in 'Python' started by kepes.krisztian, Sep 7, 2004.

  1. Hi !

    I don't understand something:

    See that code:
    class A(object):
    __slots__=('x','a','__v')
    def __init__(self):
    self.__v=0
    def g(self):
    print "g"
    return self.__v
    def s(self,v):
    self.__v=v
    print "s"
    GS=property(g,s)

    a=A()
    print a.g()
    a.s(1)
    print a.g()
    #print a.GS
    a.GS=2
    print a.GS
    print a.g()

    It is working.
    But when we remove slots, it is missing: the a.GS is not used as
    property, it is access a local member. Why ?

    class A(object):
    def __init__(self):
    self.__v=0
    def g(self):
    print "g"
    return self.__v
    def s(self,v):
    self.__v=v
    print "s"
    GS=property(g,s)

    a=A()
    print a.g()
    a.s(1)
    print a.g()

    #print a.GS
    a.GS=2
    print a.GS
    print a.g()

    Another question: why the __slots__ is working only if this member is in
    body of A ?
    Why isn't when it is in the __init__ procedure ?

    def __init__(self):
    self.__v=0
    __slots__=('x','a','__v')


    Thanx for help:
    FT
     
    kepes.krisztian, Sep 7, 2004
    #1
    1. Advertising

  2. kepes.krisztian

    Russell Blau Guest

    "kepes.krisztian" <> wrote in message
    news:...
    >
    > See that code:
    > class A(object):
    > __slots__=('x','a','__v')
    > def __init__(self):
    > self.__v=0

    ....
    > Another question: why the __slots__ is working only if this member is in
    > body of A ?
    > Why isn't when it is in the __init__ procedure ?
    >
    > def __init__(self):
    > self.__v=0
    > __slots__=('x','a','__v')
    >


    I can't answer your first question, but I can the second. In the first
    piece of code, you have defined __slots__ (correctly) as an attribute of the
    class A. In the second piece, you have created a local variable called
    __slots__ within the __init__() method. As soon as the __init__ method
    returns, that local variable is gone!! Changing it to self.__slots__
    wouldn't work either, because that would define an attribute of the
    *instance*, not of the *class*. Slots and properties belong to the class,
    not to its instances.


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
     
    Russell Blau, Sep 7, 2004
    #2
    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. walala
    Replies:
    4
    Views:
    1,203
    Technology Consultant
    Sep 9, 2003
  2. walala
    Replies:
    1
    Views:
    654
    steve
    Sep 12, 2003
  3. kpk
    Replies:
    37
    Views:
    16,716
    Bob Stephens
    Jan 6, 2004
  4. Kenny

    SOS - SourceSafe

    Kenny, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    593
    Chris Jackson
    Dec 5, 2003
  5. Replies:
    1
    Views:
    273
    Peter Hansen
    Dec 15, 2003
Loading...

Share This Page