getattr

How to iterate through a module's functions [duplicate]

醉酒当歌 提交于 2019-12-01 03:50:52
This question already has an answer here: How to list all functions in a Python module? 14 answers I have this function call after importing foo.py. Foo has several methods that I need to call e.g. foo.paint, foo.draw: import foo code if foo: getattr(foo, 'paint')() I need to use a while loop to call and iterate through all the functions foo.paint, foo.draw etc. How do i go about it? You can use foo.__dict__ somehow like this: for name, val in foo.__dict__.iteritems(): # iterate through every module's attributes if callable(val): # check if callable (normally functions) val() # call it But

Python - getattr and concatenation

此生再无相见时 提交于 2019-12-01 00:26:11
So in playing around with getattr in my code I discovered the following: myVariable = foo.A.bar works...but something like this: B = "A" myVariable = getattr(foo, B + ".bar") returns an error that foo does not contain an attribute A.bar. Where am I going wrong? Thanks! Because there is no attribute A.bar on foo . Attribute bar is a part of the object pointed to by A , which is an attribute of foo . You need either getattr(foo.A, "bar") or getattr(getattr(foo, 'A'), 'bar') The generic code for accessing deep attributes is to split on the dot, and go until the last part is found (I'm writing

Python 2 __getattr__ max recursion depth

寵の児 提交于 2019-11-30 18:06:42
问题 for example i use this code: class A(object): def __init__(self): self.dict1 = { 'A': 3, 'B': self.A} def __getattr__(self, key): if key in self.dict1: return self.dict1[key] a = A() and when it's runned it throws maximum recursion depth exceeded. Can someone please tell me what am i doing wrong here 回答1: The reference to self.dict1 inside your __getattr__ method causes __getattr__ to be called again, and so on, hence the infinite recursion. The only safe way to access attributes of self

What is the relationship between __getattr__ and getattr?

只愿长相守 提交于 2019-11-29 20:21:47
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 ? Alex's answer was good, but providing you with a sample code since you asked for it :) class foo: def __init_

Determine if __getattr__ is method or attribute call

时光怂恿深爱的人放手 提交于 2019-11-29 06:53:20
Is there any way to determine the difference between a method and an attribute call using __getattr__? I.e. in: class Bar(object): def __getattr__(self, name): if THIS_IS_A_METHOD_CALL: # Handle method call def method(**kwargs): return 'foo' return method else: # Handle attribute call return 'bar' foo=Bar() print(foo.test_method()) # foo print(foo.test_attribute) # bar The methods are not local so it's not possible to determine it using getattr/callable. I also understand that methods are attributes, and that there might not be a solution. Just hoping there is one. You cannot tell how an

'super' object not calling __getattr__

有些话、适合烂在心里 提交于 2019-11-28 09:46:31
I have one object wrapped inside another. The "Wrapper" accesses the attributes from the "Wrapped" object by overriding __getattr__ . This works well until I need to override an atribute on a sub class, and then access the attribute from the base class using super() . I can still access the attribute directly from __getattr__ but why does super() not work? class Wrapped(object): def __init__(self, value): self.value = value def hello_world(self): print 'hello world', self.value class Wrapper(object): def __init__(self, obj): self.wrapped_obj = obj def __getattr__(self, name): if name in self._

Asymmetric behavior for __getattr__, newstyle vs oldstyle classes

流过昼夜 提交于 2019-11-28 09:23:05
this is the first time I write here, sorry if the message is unfocuessed or too long. I was interested in understanding more about how objects'attributes are fetched when needed. So I read the Python 2.7 documentation titled "Data Model" here , I met __getattr__ and, in order to check whether I understood or not its behavior, I wrote these simple (and incomplete) string wrappers. class OldStr: def __init__(self,val): self.field=val def __getattr__(self,name): print "method __getattr__, attribute requested "+name class NewStr(object): def __init__(self,val): self.field=val def __getattr__(self

How to intercept instance method calls?

百般思念 提交于 2019-11-28 06:04:39
I am looking for a way to intercept instance method calls in class MyWrapper below: class SomeClass1: def a1(self): self.internal_z() return "a1" def a2(self): return "a2" def internal_z(self): return "z" class SomeClass2(SomeClass1): pass class MyWrapper(SomeClass2): # def INTERCEPT_ALL_FUNCTION_CALLS(): # result = Call_Original_Function() # self.str += result # return result def __init__(self): self.str = '' def getFinalResult(self): return self.str x = MyWrapper() x.a1() x.a2() I want to intercept all function calls make through my wrapper class. In my wrapper class I want to keep track of

getattr() versus dict lookup, which is faster?

时光怂恿深爱的人放手 提交于 2019-11-28 03:21:11
问题 A somewhat noobish, best practice question. I dynamically look up object attribute values using object.__dict__[some_key] as a matter of habit. Now I am wondering which is better/faster: my current habit or getattr(object,some_key) . If one is better, why? >>> class SomeObject: ... pass ... >>> so = SomeObject() >>> so.name = 'an_object' >>> getattr(so,'name') 'an_object' >>> so.__dict__['name'] 'an_object' 回答1: You are much better off using getattr() instead of going directly to the __dict__

Determine if __getattr__ is method or attribute call

为君一笑 提交于 2019-11-28 00:25:20
问题 Is there any way to determine the difference between a method and an attribute call using __getattr__? I.e. in: class Bar(object): def __getattr__(self, name): if THIS_IS_A_METHOD_CALL: # Handle method call def method(**kwargs): return 'foo' return method else: # Handle attribute call return 'bar' foo=Bar() print(foo.test_method()) # foo print(foo.test_attribute) # bar The methods are not local so it's not possible to determine it using getattr/callable. I also understand that methods are