Search code examples
pythonpython-2.7namedtuplegetattr

Values recovery from named tuples using list comprehension inside function with property decorator?


I'm following the python documentation and this SO answer also this but i'm getting a property object pointer when using getattr and im unable to get or print the value i'm looking for.

class DefaultSettings():
    """This contains default game settings."""
    def __init__(self):
        """Default settings"""
        self.Keyboard = namedtuple('Keyboard', ['gas', 'reverse', 'sbreak', 'ebreak', 'left', 'right'])
        self.Keyboard(gas = 'WKEY', reverse = 'SKEY', sbreak = 'SKEY', ebreak = 'SPACEKEY', left = 'AKEY', right = 'DKEY')
    @property
    def keyboard(self):
        return [getattr(self.Keyboard, key) for key in self.Keyboard._fields]

settings = DefaultSettings()
print("keyboard 1: ",settings.keyboard)
print("Keyboard 2: ",settings.Keyboard)
print("keyboard 3: ",settings.Keyboard._fields)
print("Keyboard 4: ", DefaultSettings.keyboard.__get__)
print("Keyboard 5: ", DefaultSettings.keyboard.__get__(settings, DefaultSettings))
for key in settings.Keyboard._fields:
    print(getattr(settings.Keyboard, key))

and the outputs:

D:\Games\BSR\scripts>python config.py
keyboard 1:  [<property object at 0x000001C18DDC4778>, <property object at 0x000001C18DDC4AE8>, <property object at 0x000001C18DDC4B38>, <property object at 0x000001C18DDC4B88>, <property object at 0x000001C18DDC4BD8>, <property object at 0x000001C18DDC4C28>]
Keyboard 2:  <class '__main__.Keyboard'>
keyboard 3:  ('gas', 'reverse', 'sbreak', 'ebreak', 'left', 'right')
Keyboard 4:  <method-wrapper '__get__' of property object at 0x000001C18DDC4598>
Keyboard 5:  [<property object at 0x000001C18DDC4778>, <property object at 0x000001C18DDC4AE8>, <property object at 0x000001C18DDC4B38>, <property object at 0x000001C18DDC4B88>, <property object at 0x000001C18DDC4BD8>, <property object at 0x000001C18DDC4C28>]
<property object at 0x000001C18DDC4778>
<property object at 0x000001C18DDC4AE8>
<property object at 0x000001C18DDC4B38>
<property object at 0x000001C18DDC4B88>
<property object at 0x000001C18DDC4BD8>
<property object at 0x000001C18DDC4C28>
D:\Games\BSR\scripts>

Solution

  • The problem is in your __init__ method:

    def __init__(self):
        """Default settings"""
        self.Keyboard = namedtuple('Keyboard', ['gas', 'reverse', 'sbreak', 'ebreak', 'left', 'right'])
        self.Keyboard(gas = 'WKEY', reverse = 'SKEY', sbreak = 'SKEY', ebreak = 'SPACEKEY', left = 'AKEY', right = 'DKEY')
    

    As the doc of namedtuple says:

    Returns a new tuple subclass named typename

    So in your first line of __init__ method, you init a new class and assgin it it field self.Keyboard, and in the following line you create a new instance of class self.Keyboard but assign it to nowhere, the self.Keyboard is still a class object. So what the following codes doing is iterating/printing properties and methods of a class, not an instance. I guess what you want might be:

    def __init__(self):
        Keyboard = namedtuple('Keyboard', ['gas', 'reverse', 'sbreak', 'ebreak', 'left', 'right'])
        self.Keyboard = Keyboard(gas = 'WKEY', reverse = 'SKEY', sbreak = 'SKEY', ebreak = 'SPACEKEY', left = 'AKEY', right = 'DKEY')
    

    Then you'll get the values you want.