SOS - property question

  • Thread starter kepes.krisztian
  • Start date
K

kepes.krisztian

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
 
R

Russell Blau

kepes.krisztian said:
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top