getattr

How are arguments passed to a function through __getattr__

How are arguments passed to a function through __getattr__ Question: Consider the following code example (python 2.7): class Parent: def __init__(self, child): self.child = child def __getattr__(self, attr): print(“Calling __getattr__: “+attr) if hasattr(self.child, attr): return getattr(self.child, attr) else: raise AttributeError(attr) class Child: def make_statement(self, age=10): print(“I am an instance of Child with age “+str(age)) kid …

Total answers: 2

getting dynamic attribute in python

getting dynamic attribute in python Question: 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 …

Total answers: 5

Python using getattr to call function with variable parameters

Python using getattr to call function with variable parameters Question: I’m using getattr to call different functions depending on a variable. Im doing something like that: getattr(foo, bar) () That works, calling functions like foo.bar() My problem is that I have ‘bar’ functions and I want to call it with different parameters. For example: def …

Total answers: 3

What does this (simple?) expression in Python mean? func(self)(*args)

What does this (simple?) expression in Python mean? func(self)(*args) Question: I saw some Python code like: getattr(self, that)(*args). what does it mean? I see that the builtin getattr function gets called, passing the current object and that; but what is the (*args) doing after that? Does it call something with *args as parameter? Asked By: …

Total answers: 4

How to launch getattr function in python with additional parameters?

How to launch getattr function in python with additional parameters? Question: I want to call some unknown function with adding parameters using getattr function. Is it possible? Asked By: megido || Source Answers: Yes, but you don’t pass them to getattr(); you call the function as normal once you have a reference to it. getattr(obj, …

Total answers: 3

Python: inconsistence in the way you define the function __setattr__?

Python: inconsistence in the way you define the function __setattr__? Question: Consider this code: class Foo1(dict): def __getattr__(self, key): return self[key] def __setattr__(self, key, value): self[key] = value class Foo2(dict): __getattr__ = dict.__getitem__ __setattr__ = dict.__setitem__ o1 = Foo1() o1.x = 42 print(o1, o1.x) o2 = Foo2() o2.x = 42 print(o2, o2.x) I would expect …

Total answers: 4

Understanding the difference between __getattr__ and __getattribute__

Understanding the difference between __getattr__ and __getattribute__ Question: 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 …

Total answers: 4

What is getattr() exactly and how do I use it?

What is getattr() exactly and how do I use it? Question: I read an article about the getattr function, but I still can’t understand what it’s for. The only thing I understand about getattr() is that getattr(li, "pop") is the same as calling li.pop. When and how do I use this exactly? The book said …

Total answers: 14

Difference between __getattr__ and __getattribute__

Difference between __getattr__ and __getattribute__ Question: I am trying to understand when to define __getattr__ or __getattribute__. The python documentation mentions __getattribute__ applies to new-style classes. What are new-style classes? Asked By: Yarin || Source Answers: New-style classes are ones that subclass “object” (directly or indirectly). They have a __new__ class method in addition to …

Total answers: 8

__getattr__ for static/class variables

__getattr__ for static/class variables Question: 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 …

Total answers: 5