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
566
votes
8 answers

Difference between __getattr__ and __getattribute__

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?
Yarin
  • 173,523
  • 149
  • 402
  • 512
396
votes
14 answers

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

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…
Terence Ponce
  • 9,123
  • 9
  • 31
  • 37
269
votes
4 answers

Understanding the difference between __getattr__ and __getattribute__

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…
user225312
  • 126,773
  • 69
  • 172
  • 181
224
votes
3 answers

How do I override __getattr__ without breaking the default behavior?

How do I override the __getattr__ method of a class without breaking the default behavior?
sheats
  • 33,062
  • 15
  • 45
  • 44
167
votes
4 answers

How do I call setattr() on the current module?

What do I pass as the first parameter "object" to the function setattr(object, name, value), to set variables on the current module? For example: setattr(object, "SOME_CONSTANT", 42); giving the same effect as: SOME_CONSTANT = 42 within the module…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
148
votes
8 answers

__getattr__ on a module

How can implement the equivalent of a __getattr__ on a class, on a module? Example When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
138
votes
6 answers

How do I implement __getattribute__ without an infinite recursion error?

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…
Greg
  • 45,306
  • 89
  • 231
  • 297
80
votes
5 answers

How to use __setattr__ correctly, avoiding infinite recursion

I want to define a class containing read and write methods, which can be called as follows: instance.read instance.write instance.device.read instance.device.write To not use interlaced classes, my idea was to overwrite the __getattr__ and…
Alex
  • 41,580
  • 88
  • 260
  • 469
78
votes
5 answers

__getattr__ for static/class variables

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…
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
53
votes
4 answers

Call method from string

If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it: class CallMe: # Class def App(): # Method one ... def Foo(): # Method two …
Sirupsen
  • 2,075
  • 2
  • 19
  • 24
45
votes
3 answers

How to launch getattr function in python with additional parameters?

I want to call some unknown function with adding parameters using getattr function. Is it possible?
megido
  • 4,135
  • 6
  • 29
  • 33
39
votes
3 answers

What is the relationship between __getattr__ and getattr?

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,…
zjm1126
  • 63,397
  • 81
  • 173
  • 221
32
votes
5 answers

Recursively access dict via attributes as well as index access?

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…
Luke Stanley
  • 1,274
  • 1
  • 16
  • 32
31
votes
5 answers

getting dynamic attribute in python

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…
rhormaza
  • 487
  • 1
  • 4
  • 9
30
votes
3 answers

Python using getattr to call function with variable parameters

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…
lcorral
  • 305
  • 1
  • 3
  • 5
1
2 3
25 26