getattr

How do I call setattr() on the current module?

一曲冷凌霜 提交于 2019-11-26 17:09:53
What do I pass as the first parameter " object " to the function setattr(object, name, value) , to set variables on the current module? For example: setattr(object, "SOME_CONSTANT", 42); giving the same effect as: SOME_CONSTANT = 42 within the module containing these lines (with the correct object ). I'm generate several values at the module level dynamically, and as I can't define __getattr__ at the module level, this is my fallback. import sys thismodule = sys.modules[__name__] setattr(thismodule, name, value) or, without using setattr (which breaks the letter of the question but satisfies

How to use __setattr__ correctly, avoiding infinite recursion

微笑、不失礼 提交于 2019-11-26 12:45:53
问题 I want to define a class containing read and write methode, which can be called as follows: instance.read instance.write instance.device.read instance.device.write To not use interlaced classes, my idea was to overwrite the __getattr__ and __setattr__ methods and to check, if the given name is device to redirect the return to self . But I encountered a problem giving infinite recursions. The example code is as follows: class MyTest(object): def __init__(self, x): self.x = x def __setattr__

__getattr__ for static/class variables in python

末鹿安然 提交于 2019-11-26 10:39:21
问题 I have a class like: class MyClass: Foo = 1 Bar = 2 Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python? I know it is possible if I create an instance of the class and I can define my own __getattr__ method. But my scnenario involves using this class as such without creating any instance of it. Also I need a custom __str__ method to be invoked when str(MyClass.Foo) is invoked. Does Python provide such an

How do I override __getattr__ in Python without breaking the default behavior?

左心房为你撑大大i 提交于 2019-11-26 10:07:15
问题 I want to override the __getattr__ method on a class to do something fancy but I don\'t want to break the default behavior. What\'s the correct way to do this? 回答1: Overriding __getattr__ should be fine -- __getattr__ is only called as a last resort i.e. if there are no attributes in the instance that match the name. For instance, if you access foo.bar , then __getattr__ will only be called if foo has no attribute called bar . If the attribute is one you don't want to handle, raise

How do I call setattr() on the current module?

别等时光非礼了梦想. 提交于 2019-11-26 05:17:14
问题 What do I pass as the first parameter \" object \" to the function setattr(object, name, value) , to set variables on the current module? For example: setattr(object, \"SOME_CONSTANT\", 42); giving the same effect as: SOME_CONSTANT = 42 within the module containing these lines (with the correct object ). I\'m generate several values at the module level dynamically, and as I can\'t define __getattr__ at the module level, this is my fallback. 回答1: import sys thismodule = sys.modules[__name__]

Difference between __getattr__ vs __getattribute__

与世无争的帅哥 提交于 2019-11-26 01:27:57
问题 I am trying to understand when to use __getattr__ or __getattribute__ . The documentation mentions __getattribute__ applies to new-style classes. What are new-style classes? 回答1: A key difference between __getattr__ and __getattribute__ is that __getattr__ is only invoked if the attribute wasn't found the usual ways. It's good for implementing a fallback for missing attributes, and is probably the one of two you want. __getattribute__ is invoked before looking at the actual attributes on the

__getattr__ on a module

て烟熏妆下的殇ゞ 提交于 2019-11-26 00:41:54
问题 How can implement the equivalent of a __getattr__ on a class, on a module? Example When calling a function that does not exist in a module\'s statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed in the attribute lookup on the module. class A(object): def salutation(self, accusative): print \"hello\", accusative # note this function is intentionally on the module, and not the class above def __getattr__

__getattr__ on a module

我是研究僧i 提交于 2019-11-25 16:51:34
How can implement the equivalent of a __getattr__ on a class, on a module? Example When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed in the attribute lookup on the module. class A(object): def salutation(self, accusative): print "hello", accusative # note this function is intentionally on the module, and not the class above def __getattr__(mod, name): return getattr(A(), name) if __name__ == "__main__": # i hope here to have my __getattr__