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

Why does setattr not affect the return value?

I am using setattr() to monkey patch a closure. Using getattr() shows that the closure was patched correctly. However, the return value is unaffected. >>> def foo(): ... def bar(): ... return('bar') ... return(bar()) ... >>> def…
0
votes
1 answer

Why is __getattr__catching also existing attributes?

I created this class to test some of the __getattr__ features: class Trace: def __init__(self, val): self.val = val def __setattr__(self, attr, value): print('set ' + attr) def __getattr__(self, attr): …
PMM
  • 366
  • 1
  • 10
0
votes
1 answer

AttributeError: 'super' object has no attribute '__getattr__' ( I searched, but to no avail)

I'm trying to make a code to calculate how much paint is needed to paint the whole room. My coding: # main.py from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen from kivy.properties…
0
votes
2 answers

Getting real caller method name when using setattr()

I have a simple class in which I want to generate methods based on inherited class fields: class Parent: def __init__(self, *args, **kwargs): self.fields = getattr(self, 'TOGGLEABLE') self.generate_methods() def…
wencakisa
  • 5,850
  • 2
  • 15
  • 36
0
votes
2 answers

How to use pandas.DataFrame.apply with getattr function in Python

Suppose I'd like to remove '$' signs from my dataframe in Pandas. And I have created a class called TransformFunctions so that I can use getattr() to invoke function from that class (the reason being that I am using another JSON file in which I will…
user1330974
  • 2,500
  • 5
  • 32
  • 60
0
votes
1 answer

Unable utilize a methodname(from webservice)present in the form of a string.I tried utilizing getattr,but then not unable to extend it for suds

from suds.client import Client #@UnresolvedImport from suds.transport.https import HttpAuthenticated #@UnresolvedImport import urllib2 class methodinvokeclass(): def methodinvokemethod(self,*args): method=args[1] c=args[2] …
TheDodo
  • 53
  • 1
  • 3
0
votes
1 answer

Using the __dict__ attribute in __getattr__

Here is a simple class with a customized __getattr__ implementation: class Wrapper(object): def __init__(self, obj): self.obj = obj def __getattr__(self, name): func = getattr(self.__dict__['obj'], name) if…
Albert
  • 2,146
  • 10
  • 32
  • 54
0
votes
2 answers

Overwriting __getattr__ causing recursion

I'm trying to create a wrapper class and then overwrite __getattr__ so that requests for attributes that are not in the wrapper class are passed through into the inner object. Below is a toy example where I used a list as the inner object - class…
RoachLord
  • 993
  • 1
  • 14
  • 28
0
votes
1 answer

Why using __dict__ in __setattr__ causing infinite loop in __getattr__

I wrote a working program for this specified simplified Mesh class, but I can not make it work for real class with dozens of methods/properties. I can not modify real Mesh class, and I can not make Object class extended Mesh. This works fine: …
0
votes
1 answer

How to use getattr() with dynamic object?

I need to get the attribute of different objects to print your content. I thought about using the object argument dynamically, is it possible? content = getattr("%s" % (variable), "attribute")
0
votes
1 answer

Python - use schedule module, using parameters from file

I'm wish to use the schedule module, as shown here, in a way that code read schedule parameters from a text file, for example: start_day=1,2,3,4 start_time=09:00 module's syntax is as follows (wish to change days, and…
guyd
  • 693
  • 2
  • 14
  • 32
0
votes
1 answer

Counting used attributes at runtime

I'm working on a python project that requires me to compile certain attributes of some objects into a dataset. The code I'm currently using is something like the following: class VectorBuilder(object): SIZE = 5 def __init__(self, player,…
Mate de Vita
  • 1,102
  • 12
  • 32
0
votes
1 answer

Odd getattr() behaviour with class property in the default

Here I expected that all 4 IDs will be the same, however it seems that when I pass class property to the default attribute of the getattr, it simply skips the attribute lookup and always return the default: Python 3.6.5 (default, May 11 2018,…
NarūnasK
  • 4,564
  • 8
  • 50
  • 76
0
votes
1 answer

Getting memory location from getattr()

from my research, I understand that using getattr() allows one to get a method on a certain object such as: getattr(obj, method) is the equivalent to obj.method() However, I tried to use it in my code and the statement returns a memory location:…
M.Bris
  • 35
  • 1
  • 2
  • 8
0
votes
1 answer

How to dynamically call class.function(value) in python 3

OK, so I have a string, x = module.class.test_function(value), and I want to call it and get the response. I've tried to use getattr(module.class, test_function)(value) yet it gives the error: AttributeError: module 'module' has no attribute…
marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37