Skip to content Skip to sidebar Skip to footer

Python Decorator Also For Undefined Attributes

I'd like to create a Model Class for an User. The data of the user are stored in an document based database like couchdb or mongodb. The class User should have an decorator and the

Solution 1:

You need to implement the __getattr__(self, name) method.


Solution 2:

Access to an object's attributes is governed by the getattr/setattr/delattr/getattribute mechanism.


Solution 3:


Solution 4:

I think i found a solution based on your suggestions:

def DocumentDB(object):
    class TransparentAttribute:
        def __init__(self, *args, **kargs):                 
            self.wrapped = object(*args, **kargs)
        def __getattr__(self, attrname):
            return "Any Value"
    return TransparentAttribute

@DocumentDB
class User(object):
    def doSomething(self):
        pass
    def doSomethingElse(self):
        pass

u = User()
print u.emailAddress
print u.lastName

It works, but is it the most pythoniastic way?


Post a Comment for "Python Decorator Also For Undefined Attributes"