getattr

What is the difference between type.__getattribute__ and object.__getattribute__?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 05:54:06
问题 Given: In [37]: class A: ....: f = 1 ....: In [38]: class B(A): ....: pass ....: In [39]: getattr(B, 'f') Out[39]: 1 Okay, that either calls super or crawls the mro? In [40]: getattr(A, 'f') Out[40]: 1 This is expected. In [41]: object.__getattribute__(A, 'f') Out[41]: 1 In [42]: object.__getattribute__(B, 'f') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-42-de76df798d1d> in <module>() ----> 1

How to launch getattr function in python with additional parameters?

给你一囗甜甜゛ 提交于 2019-12-31 08:44:32
问题 I want to call some unknown function with adding parameters using getattr function. Is it possible? 回答1: 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) 回答2: If you wish to invoke a dynamic method with a dynamic list of arguments / keyword arguments, you can do the following: function_name = 'wibble' args = ['flip', 'do'] kwargs = {'foo':'bar'} getattr(obj, function_name)(*args, **kwargs) 来源:

python getattr built-in method executes default arguments

雨燕双飞 提交于 2019-12-31 02:01:05
问题 I dont know if this is something an expected behavior of getattr built_in method. getattr executes the default(3rd) argument as well even if the actual argument(2nd) satisfies the condition. Example: def func(): print('In Function') class A: def __init__(self): self.param = 12 a = A() When I run getattr(a, 'param', func()) it gives this result: In Function 12 Note the In Function which I don't want. But it works perfectly fine when I execute getattr(a, 'param1', func()) i.e output In Function

How can I reach a private variable within the object

一曲冷凌霜 提交于 2019-12-31 01:50:08
问题 I would like to modify an object private variable class Example(): __myTest1 = 1 __myTest2 = 1 def __init__(self): pass def modifyTest(self, name = 'Test1', value): setattr(self, '__my'+name, value); I tried the code above and it's seems that not possible to reach a private variable, AttributeError: Example instance has no attribute '__myTest1' Is there any way to modify a private variable? 回答1: Accessing from outside: e = Example() e._Example__myTest1 # 1 Due to private variable name

What is the relationship between __getattr__ and getattr?

本秂侑毒 提交于 2019-12-29 11:00:08
问题 I know this code is right: class A: def __init__(self): self.a = 'a' def method(self): print "method print" a = A() print getattr(a, 'a', 'default') print getattr(a, 'b', 'default') print getattr(a, 'method', 'default') getattr(a, 'method', 'default')() And this is wrong: # will __getattr__ affect the getattr? class a(object): def __getattr__(self,name): return 'xxx' print getattr(a) This is also wrong: a={'aa':'aaaa'} print getattr(a,'aa') Where should we use __getattr__ and getattr ? 回答1:

Understanding the difference between __getattr__ and __getattribute__

时光毁灭记忆、已成空白 提交于 2019-12-28 01:39:40
问题 I am trying to understand the difference between __getattr__ and __getattribute__ , however, I am failing at it. The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says: __getattribute__ is invoked before looking at the actual attributes on the object, and so can be tricky to implement correctly. You can end up in infinite recursions very easily. I have absolutely no idea what that means. Then it goes on to say: You almost certainly want __getattr__ .

The Python getattr Function

青春壹個敷衍的年華 提交于 2019-12-26 20:00:35
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Python年代getattr函数用于获取一个属性的对象,使用字符串对象,而不是一个标识符识别属性。换句话说,以下两个语句是等价的 value = obj.attribute value = getattr(obj, "attribute" ) 如果属性存在,返回相应的值。如果属性不存在,你得到一个AttributeError异常。 The getattr function can be used on any object that supports dotted notation (by implementing the __getattr__ method). This includes class objects, modules, and even function objects. getattr函数可用于任何支持点状符号的对象(通过实现 __getattr__ 方法)。这包括类对象、模块和函数对象 path = getattr(sys, "path" ) doc = getattr(len, "__doc__" ) The getattr function uses the same lookup rules as ordinary attribute access, and you can

strange behavior with lamba: getattr(obj, x) inside a list [duplicate]

江枫思渺然 提交于 2019-12-25 07:39:05
问题 This question already has answers here : Weird behavior: Lambda inside list comprehension (6 answers) Closed 6 years ago . In the following example: class A(object): pass prop1 = 1 prop2 = 2 prop3 = 3 prop4 = 4 obj = A() tmp = ['prop1', 'prop2', 'prop3', 'prop4'] getter = [ lambda: getattr(obj, x) for x in tmp ] I am always getting 4 when calling the getter : [getter[i]() for i in range(4)] #[4, 4, 4, 4] why!? 回答1: This is a very common problem with lambdas. Ultimately, the variable x is

can you use getattr to call a function within your scope?

孤街浪徒 提交于 2019-12-23 18:29:11
问题 I'm trying to do something like this, but I can't figure out how to call the function bar . def foo(): def bar(baz): print('used getattr to call', baz) getattr(bar, __call__())('bar') foo() Notice, that this is somewhat unusual. Normally you'd have an object and get an attribute on that, which could be a function. then it's easy to run. but what if you just have a function within the current scope - how to do getattr on the current scope to run the function? 回答1: You are close. To use getattr

__getattr__ equivalent for methods

那年仲夏 提交于 2019-12-23 11:54:15
问题 When an attribute is not found object.__getattr__ is called. Is there an equivalent way to intercept undefined methods? 回答1: Methods are attributes too. __getattr__ works the same for them: class A(object): def __getattr__(self, attr): print attr Then try: >>> a = A() >>> a.thing thing >>> a.thing() thing Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable 回答2: There is no difference. A method is also an attribute. (If you want