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
-1
votes
1 answer

Python reflection, getter to methods

class A: def __init__(self): self.var = 10 def print(self): print("hiiiiii") For an object of A, we can easily access the attributes with getattr and setattr. Is there any way in Python to have an instance of class and a…
-1
votes
1 answer

Using getattr() on self

I have a class and within one of the methods of the class I have a string given from user input which is then mapped to the corresponding method (technically is a str representation of the method). How can I call this method without an instance of…
-1
votes
1 answer

Why __getattr__ is called instead of catching AttributeError?

I have implemented a simple lazy initialization in Python class: class Base: def __init__(self): pass def __getattr__(self, item): print('__getattr__ was called!') class Derived(Base): def GetValue(self): try: …
Michal
  • 627
  • 3
  • 13
-1
votes
1 answer

Using __setattr__ method with getattr in a class

I have a Python class as follows: class Base: def __init__(self, response=20): self.response = response def func1(self): self.response = 10 def func2(self): self.response = 20 def func3(self): …
Amistad
  • 7,100
  • 13
  • 48
  • 75
-1
votes
1 answer

getattr is returning character '>' instead of mehtod

I have a class defined in feat.py class feat: def __init__(self): print 'feat init ' pass def do_something(self): return true Now I am calling the following: from feat import * f=feat() for i in dir(f): #feature_functions: …
bikas
  • 9
  • 1
-1
votes
1 answer

get Attr from Maya shotlist

I want to get the information from the fields in the shotlist (in the camera sequencer). The shotname I've solved: test = cmds.getAttr('shot1.sn') print test But the rest.. I'm stuck. When I try to call the other arguments like startTime I get all…
Fantasi
  • 3
  • 1
-1
votes
1 answer

Values recovery from named tuples using list comprehension inside function with property decorator?

I'm following the python documentation and this SO answer also this but i'm getting a property object pointer when using getattr and im unable to get or print the value i'm looking for. class DefaultSettings(): """This contains default game…
Strapicarus
  • 131
  • 1
  • 5
-1
votes
1 answer

Dynamically set dict elements with getattr

I already see the post Dynamically get dict elements via getattr?, but i can't solve my problem. I want do something similar, but I'm a bit confused. I want set (not get) the data in the correspondent dictionary, but I get this error…
benpay
  • 61
  • 1
  • 8
-1
votes
1 answer

Python: why visiting an existing attribute triggered "__getattr__"?

I knew that python's __getattr__ is triggered when visiting a non-existing attribute. But in my example below, inside c1's __init__, I created a self attribute called name. When visiting it, both access ways triggered __getattr__ and thus printed…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
-1
votes
1 answer

How to know which attribute and object has a specified value

I'm a newbie with python, and would really appreciate your help with my issue. I have a class A and three subclasses B, C, D, which all inherit from A - like this. class A: def __init__(self, arg1, arg2, arg3): self.attr1 = B …
-1
votes
1 answer

Python getattr from locals() fails with AttributeError

Hopefully this is not the late-night effect, but I'm stumped by a very simple problem: def test_getattr(v0): v1 = "test1" v2 = "test2" v3 = "test3" for k, v in locals().items(): print(k, v) print(getattr(locals(),…
GoneAsync
  • 349
  • 5
  • 18
-1
votes
1 answer

How to implement __getattr__ method without breaking default behaviour in python classic class

I want to add some logging to __getattr__ method in python classic class. It work well normally, but when using operator overloading, something was wrong. My code follows: class A: def __getattr__(self, item): print('attr:'+ item) …
Xiangyu.Wu
  • 459
  • 1
  • 5
  • 18
-2
votes
2 answers

Execute python method from a string

I have a string that has the method name and value in it. Currently, I'm using the following implementation which works but doesn't seem elegant. Is there a better way to implement this? class ObjectResolver: def methodResolver(self, value): …
-2
votes
1 answer

Selenium Python - GetAttr

I'm trying to do an automation web test with Selenium Python. Where I could read the xpath of an WebElement and/or the method I want to do with it for example send_keys() from an Excel file. I managed to make the code works, but problem is the…
-2
votes
2 answers

Use of getattr on a class with data descriptor

While using data descriptors in building classes, I came across a strange behavior of getattr function on a class. # this is a data descriptor class String(object): def __get__(self, instance, owner): pass def __set__(self, instance,…
Jaewan Kim
  • 45
  • 5
1 2 3
25
26