getattr

getting dynamic attribute in python [duplicate]

自古美人都是妖i 提交于 2019-11-27 22:48:46
This question already has an answer here: How to access object attribute given string corresponding to name of that attribute 2 answers I have and object with an pseudo or special attribute that can be named in three different ways (Note: I don't control the code which generates the object) The value in the attributes (depending which one is set) is exactly the same, and I need to get that for further processing, so depending of the source of data, I can have something like: >>> obj.a 'value' >>> obj.b Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Obj

Recursively access dict via attributes as well as index access?

折月煮酒 提交于 2019-11-27 10:32:21
I'd like to be able to do something like this: from dotDict import dotdictify life = {'bigBang': {'stars': {'planets': []} } } dotdictify(life) # This would be the regular way: life['bigBang']['stars']['planets'] = {'earth': {'singleCellLife': {}}} # But how can we make this work? life.bigBang.stars.planets.earth = {'singleCellLife': {}} #Also creating new child objects if none exist, using the following syntax: life.bigBang.stars.planets.earth.multiCellLife = {'reptiles':{},'mammals':{}} My motivations are to improve the succinctness of the code, and if possible use similar syntax to

Querying for entities with missing properties in app engine Datastore?

社会主义新天地 提交于 2019-11-27 07:52:36
问题 I have a model which looks like this: class Example (db.Model) : row_num = db.IntegerProperty(required=True) updated = db.IntegerProperty() ... ... Now when i store values, I may not fill the value for the updated property every time, which implies that in some entities it may not exist. I want to construct a datastore query so that i can get all entities of kind Example which do not have the property updated set. How do i do this? p.s. i know i can set a default value and then query on it.

How to intercept instance method calls?

青春壹個敷衍的年華 提交于 2019-11-27 05:37:58
问题 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

__getattr__ for static/class variables in python

自闭症网瘾萝莉.ら 提交于 2019-11-27 03:42:31
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 option? Matt Anderson __getattr__() and __str__() for an object are found on its class, so if you want

Asymmetric behavior for __getattr__, newstyle vs oldstyle classes

和自甴很熟 提交于 2019-11-27 02:51:17
问题 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__,

Understanding the difference between __getattr__ and __getattribute__

巧了我就是萌 提交于 2019-11-27 02:29:13
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__ . Why? I read that if __getattribute__ fails, __getattr__ is called. So why are there two different

getting dynamic attribute in python [duplicate]

可紊 提交于 2019-11-26 23:13:30
问题 This question already has an answer here: How to access object attribute given string corresponding to name of that attribute 2 answers I have and object with an pseudo or special attribute that can be named in three different ways (Note: I don't control the code which generates the object) The value in the attributes (depending which one is set) is exactly the same, and I need to get that for further processing, so depending of the source of data, I can have something like: >>> obj.a 'value'

How do I implement __getattribute__ without an infinite recursion error?

那年仲夏 提交于 2019-11-26 21:26:45
I want to override access to one variable in a class, but return all others normally. How do I accomplish this with __getattribute__ ? I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error: class D(object): def __init__(self): self.test=20 self.test2=21 def __getattribute__(self,name): if name=='test': return 0. else: return self.__dict__[name] >>> print D().test 0.0 >>> print D().test2 ... RuntimeError: maximum recursion depth exceeded in cmp Egil You get a recursion error because your attempt to access the self.__dict__ attribute inside _

Recursively access dict via attributes as well as index access?

♀尐吖头ヾ 提交于 2019-11-26 17:57:11
问题 I'd like to be able to do something like this: from dotDict import dotdictify life = {'bigBang': {'stars': {'planets': []} } } dotdictify(life) # This would be the regular way: life['bigBang']['stars']['planets'] = {'earth': {'singleCellLife': {}}} # But how can we make this work? life.bigBang.stars.planets.earth = {'singleCellLife': {}} #Also creating new child objects if none exist, using the following syntax: life.bigBang.stars.planets.earth.multiCellLife = {'reptiles':{},'mammals':{}} My