views:

95

answers:

2

my code run wrong,i don't know why

class a:
    def __get__(self):
        return 'xxx'
    def aa(self):
        print 'aaaa'

b=a()
print b.get('aa')

Please try to use the code, rather than text, because my English is not very good, thank you

#
class HideX(object):
    def __init__(self, x):
        self.x = x

    def get_x(self):
        return self.__x

    def set_x(self, x):
        self.__x = x+10

    x = property(get_x, set_x)

inst = HideX(20)
print inst.x
inst.x = 30
print inst.x
A: 

I think you should read a bit more on Descriptors before you try to use them.

pkit
What's the difference between your twos' last lines?
Boldewyn
Huh? I thought it read 'print a.get('aa')'. I'll change my answer
pkit
It did read `print a.get('aa')` in the beginning, but the OP fixed it.
sth
A: 

You are calling obj.get, but there is no get function in class a, hence error, either rename get to get or if you by chance are trying to use descriptors do something like this

class A(object):
    def __get__(self, obj, klass):
        print "__get__", obj, klass
        return 'xxx'

class X(object):
    a = A()

x=X()
print x.a
Anurag Uniyal