English 中文(简体)
如何使属性类创建真正的属性?
原标题:How can make property class create real attribute?

使用描述符管理属性:

class Geeks:
    def __init__(self,value):
        self.age = value
    def __getattribute__(self,attr):
        return  object.__getattribute__(self,attr)
    def __setattr__(self,attr,value):
        return  object.__setattr__(self, attr,value)

初始化具有真实属性age10的Geeks类:

boy = Geeks(10)
boy.age
10
boy.__dict__
{ age : 10}

The instance boy contains a real attribute age.
In almost every book,use property class to mange the age attribute(some in the form of self.__age or self.age_ or self._age ,it does not matter):

class Geeks:
     def __init__(self,value):
          self._age = value  
     def get_age(self):
         return self._age       
     def set_age(self, value):
         self._age = value 
     age = property(get_age, set_age) 

显示属性:

boy = Geeks(10)
boy.age
10
boy.__dict__
{ _age : 10}

真实属性是_age,而不是age_ageage属性(get_age,set_age)创建真实的age属性时,我遇到了RecursionError

class Geeks:
     def __init__(self,value):
          self.age = value  
     def get_age(self):
         return self.age       
     def set_age(self, value):
         return self.age = value 
     age = property(get_age, set_age) 

相同错误:

class Geeks:
     def __init__(self,value):
          self.age = value  
     def get_age(self):
         return  object.__getattribute__(self, age )       
     def set_age(self, value):
         object.__setattr__(self,  age ,value)
     age = property(get_age, set_age) 

如何使属性(get_age,set_age)创建真正的属性age,而不是_age或类似的属性?

问题回答
class Geeks:
     def __init__(self,value):
         self.age = value  
     def get_age(self):
         return self.__dict__[ age ]       
     def set_age(self, value):
         self.__dict__[ age ] = value
     age = property(get_age, set_age) 

Geeks可以创建一个真实属性,属性名称与__dict__中的属性名称相同。

boy = Geeks(10)
in set
boy.age
in get
10
boy.__dict__
{ age : 10}




相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签