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
5
votes
2 answers

Python, Django, how to use getattr (or other method) to call object that has multiple attributes?

After trying to get this to work for a while and searching around I am truly stumped so am posting here... I want to make some functions in classes that I am writing for django as generic as possible so I want to use getattr to call functions such…
Rick
  • 16,612
  • 34
  • 110
  • 163
5
votes
3 answers

AttributeErrors: undesired interaction between @property and __getattr__

I have a problem with AttributeErrors raised in a @property in combination with __getattr__() in python: Example code: >>> def deeply_nested_factory_fn(): ... a = 2 ... return a.invalid_attr ... >>> class Test(object): ... def…
ARF
  • 7,420
  • 8
  • 45
  • 72
5
votes
6 answers

Pythonic solution to my reduce getattr problem

I used to use reduce and getattr functions for calling attributes in a chain way like "thisattr.thatattr.blaattar" IE: reduce(getattr, 'xattr.yattr.zattr'.split('.'), myobject) Works perfectly fine, however now I have a new requirement, my strings…
Hellnar
  • 62,315
  • 79
  • 204
  • 279
5
votes
1 answer

Using getattr function on self in python

I am trying to write call multiple functions through a loop using the getattr(...). Snippet below: class cl1(module): I =1 Name= 'name'+str(I) Func= 'func'+str(I) Namecall = gettattr(self,name) Namecall =…
user3548782
  • 81
  • 1
  • 1
  • 2
5
votes
2 answers

Python pickle got acycle recursion with getattr?

class Test(object): def __init__(self, a): self.a = a def __getattr__(self, name): return getattr(self.a, name) from pickle import loads, dumps loads(dumps((Test(something),))) I got: 7 def __getattr__(self,…
Jiangfan Du
  • 137
  • 1
  • 5
5
votes
3 answers

getattr on class objects

class A: def foo(self): print "foo()" getattr(A, foo) # True A.foo() # error getattr(A(), foo) # True A().foo() # prints "foo()" That being said, here is my problem: I wish to store test case meta information as attributes of the Test…
waldol1
  • 1,841
  • 2
  • 18
  • 22
5
votes
6 answers

Calling types via their name as a string in Python

I'm aware of using globals(), locals() and getattr to referance things in Python by string (as in this question) but unless I'm missing something obvious I can't seem to use this with calling types. e.g.: In [12]:…
mavnn
  • 9,101
  • 4
  • 34
  • 52
4
votes
2 answers

PHP approach to python's magic __getattr__()

I was wondering if there was some way in PHP to duplicate some of the magic of Python attribute/key access. I use a Mongo ORM class written by Steve Lacey called Minimongo in which he utilizes the __getattr__ and __getitem__ to reroute key and…
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
4
votes
1 answer

"TypeError: getattr(): attribute name must be string" in PyTorch diffusers, how to fix it?

I am trying the diffusers of Pytorch to generate pictures in my Mac M1. I have a simple syntax like this: modelid = "CompVis/stable-diffusion-v1-4" device = "cuda" pipe = StableDiffusionPipeline.from_pretrained(modelid, revision="fp16",…
yts61
  • 1,142
  • 2
  • 20
  • 33
4
votes
4 answers

Python - How to define attributes not affected by __getattr__?

I'm fairly new to Python. In programming a lot of PHP recently I got used to some creative use of __get and __set "magic" methods. These were only called when a public variable of the class wasn't present. I'm trying to replicate the same behavior…
Kyle Johnson
  • 1,605
  • 2
  • 17
  • 26
4
votes
4 answers

Are there getattr, callable and other metaprogramming functions in java?

I'm looking for some metaprogramming functions in Java analogous to Python's getattr, hasattr, callable etc. If not, is there any good external library for this?
abc def foo bar
  • 2,360
  • 6
  • 30
  • 41
4
votes
2 answers

Python's getattr gets called twice?

I am using this simple example to understand Python's getattr function: In [25]: class Foo: ....: def __getattr__(self, name): ....: print name ....: ....: In [26]: f = Foo() In [27]: f.bar bar bar Why is…
thebossman
  • 4,598
  • 11
  • 34
  • 45
4
votes
1 answer

Infinite recursion for __getattr__() - but why is it even called once?

Well I have a class that consist of (relevant part): class satellite(object): def __init__(self, orbit, payload=None, structural_mass=0, structural_price=0): self.__all_parts = [] self.orbit = orbit self.payload =…
paul23
  • 8,799
  • 12
  • 66
  • 149
4
votes
2 answers

Python Inheritance and __getattr__ & __getattribute__

I have fought with this all day long and done plenty of Google searches. I am having what appears to be an inheritance problem. I have a class named BaseClass that does some simple things for me like set logging defaults, hold the log, and manage…
Gabe Spradlin
  • 1,937
  • 4
  • 23
  • 47
4
votes
3 answers

MAYA Python: getting the position of selected object using getAttr

I am trying to make a simple "allign tool" in maya using Python script, and this is how far I got import maya.cmds as cmds selected = cmds.ls(selection=True) for all in selected: cmds.getAttr('Cube.translateX') And this seems to get the…
Chico Spans
  • 41
  • 1
  • 1
  • 2