Questions tagged [getattr]

getattr is a Python built-in function used to access a named attribute on an object.

The function's schematics are as follows:

getattr(object:object, name:str[, default:object]) -> value

where object is the object, name is the named attribute, and default, if supplied, is the default value to return if the attribute can not be found. If default is not supplied and the object can not be found, an AttributeError is thrown.

Below is a demonstration of the function's features:

>>> class Test:
...     def __init__(self):
...         self.attr = 1
...
>>> myTest = Test()
>>> getattr(myTest, 'attr')
1
>>> getattr(myTest, 'attr2', 'No attribute by that name')
'No attribute by that name'
>>> getattr(myTest, 'attr2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Test instance has no attribute 'attr2'
>>>
378 questions
22
votes
1 answer

How to iterate through a module's functions

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…
lobjc
  • 2,751
  • 5
  • 24
  • 30
20
votes
1 answer

'super' object not calling __getattr__

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…
murphy.tim
  • 203
  • 1
  • 2
  • 4
18
votes
3 answers

What are the names of the magic methods for the operators "is" and "in"?

I would like to make bool binary operations using the magic methods for these operators. For example, I can get a < b as getattr(a, '__lt__')(b) or a == b as getattr(a, '__eq__')(b). Can I get a in b and a is b in such a way?
nik
  • 265
  • 2
  • 9
18
votes
1 answer

getattr() versus __dict__ lookup, which is faster?

I can dynamically look up object attribute values using object.__dict__[some_key] or getattr(object, some_key). Which is faster or better, and why? >>> class SomeObject: ... pass ... >>> so = SomeObject() >>> so.name = 'an_object' >>>…
Cole
  • 2,489
  • 1
  • 29
  • 48
17
votes
3 answers

__getattr__ equivalent for methods

When an attribute is not found object.__getattr__ is called. Is there an equivalent way to intercept undefined methods?
hoju
  • 28,392
  • 37
  • 134
  • 178
16
votes
2 answers

How are arguments passed to a function through __getattr__

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): …
Alex
  • 41,580
  • 88
  • 260
  • 469
15
votes
1 answer

Overriding __getattr__ to support dynamic nested attributes

What is the best approach to take if you want to dynamically create and reference nested attributes? I was writing a simple Flickr client, and wanted to match the documented API as closely as possible, without actually defining every method. For…
Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
15
votes
4 answers

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

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 =…
Albert
  • 65,406
  • 61
  • 242
  • 386
15
votes
2 answers

assign a attribute using getattr

I try to assign value to attributes of some calss like the following: for name in dir(modelType): if request.get(name): getattr(model, name) = request.get(name) but get the excption: "can't assign to function call" how can I…
Alon Gutman
  • 865
  • 2
  • 14
  • 22
14
votes
4 answers

Using __getattribute__ or __getattr__ to call methods in Python

I am trying to create a subclass which acts as a list of custom classes. However, I want the list to inherit the methods and attributes of the parent class and return a sum of the quantities of each item. I am attempting to do this using the…
AJ Medford
  • 323
  • 1
  • 2
  • 10
14
votes
5 answers

Get nested attribute from a python object

I have an object with nested attributes like so: obj: attr_1: attr_2 When both attr_1 and attr_2 exists, I can get it like so: obj.attr_1.attr_2 But what if I'm not sure if either attribute exists? In this case, construction of…
Nikita Vstovsky
  • 328
  • 3
  • 9
14
votes
3 answers

Determine if __getattr__ is method or attribute call

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…
Robin Orheden
  • 2,714
  • 23
  • 24
13
votes
2 answers

python: cooperative supercall of __getattr__

I'm working with somethign similar to this code: class BaseClass(object): def __getattr__(self, attr): return lambda:'1' class SubClass(BaseClass): def foo(self): suffix = '2' return super(SubClass, self).foo() +…
bukzor
  • 37,539
  • 11
  • 77
  • 111
12
votes
3 answers

Why doesn't Python have a hybrid getattr + __getitem__ built in?

I have methods that accept dicts or other objects and the names of "fields" to fetch from those objects. If the object is a dict then the method uses __getitem__ to retrieve the named key, or else it uses getattr to retrieve the named attribute.…
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
12
votes
3 answers

How to intercept instance method calls?

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…
Vikas Vikas
  • 121
  • 1
  • 4
1
2
3
25 26