getting dynamic attribute in python [duplicate]
How about: for name in ‘a’, ‘b’, ‘c’: try: thing = getattr(obj, name) except AttributeError: pass else: break
How about: for name in ‘a’, ‘b’, ‘c’: try: thing = getattr(obj, name) except AttributeError: pass else: break
Here’s one way to create that kind of experience: class DotDictify(dict): MARKER = object() def __init__(self, value=None): if value is None: pass elif isinstance(value, dict): for key in value: self.__setitem__(key, value[key]) else: raise TypeError(‘expected dict’) def __setitem__(self, key, value): if isinstance(value, dict) and not isinstance(value, DotDictify): value = DotDictify(value) super(DotDictify, self).__setitem__(key, value) def __getitem__(self, key): … Read more
Objects in Python can have attributes — data attributes and functions to work with those (methods). Actually, every object has built-in attributes (try dir(None), dir(True), dir(…), dir(dir) in Python console). For example you have an object person, that has several attributes: name, gender, etc. You access these attributes (be it methods or data objects) usually … Read more
Alex’s answer was good, but providing you with a sample code since you asked for it 🙂 class foo: def __init__(self): self.a = “a” def __getattr__(self, attribute): return “You asked for %s, but I’m giving you default” % attribute >>> bar = foo() >>> bar.a ‘a’ >>> bar.b “You asked for b, but I’m giving … Read more
Yes, but you don’t pass them to getattr(); you call the function as normal once you have a reference to it. getattr(obj, ‘func’)(‘foo’, ‘bar’, 42)
You can do this: getattr(CallMe, variable)() getattr is a builtin method, it returns the value of the named attributed of object. The value in this case is a method object that you can call with ()
__getattr__() and __str__() for an object are found on its class, so if you want to customize those things for a class, you need the class-of-a-class. A metaclass. class FooType(type): def _foo_func(cls): return ‘foo!’ def _bar_func(cls): return ‘bar!’ def __getattr__(cls, key): if key == ‘Foo’: return cls._foo_func() elif key == ‘Bar’: return cls._bar_func() raise AttributeError(key) … Read more
You must call the parent class __setattr__ method: class MyTest(object): def __init__(self, x): self.x = x def __setattr__(self, name, value): if name==”device”: print “device test” else: super(MyTest, self).__setattr__(name, value) # in python3+ you can omit the arguments to super: #super().__setattr__(name, value) Regarding the best-practice, since you plan to use this via xml-rpc I think this … Read more
You get a recursion error because your attempt to access the self.__dict__ attribute inside __getattribute__ invokes your __getattribute__ again. If you use object‘s __getattribute__ instead, it works: class D(object): def __init__(self): self.test=20 self.test2=21 def __getattribute__(self,name): if name==’test’: return 0. else: return object.__getattribute__(self, name) This works because object (in this example) is the base class. By … Read more
There are two basic problems you are running into here: __xxx__ methods are only looked up on the class TypeError: can’t set attributes of built-in/extension type ‘module’ (1) means any solution would have to also keep track of which module was being examined, otherwise every module would then have the instance-substitution behavior; and (2) means … Read more