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

Django template not recognizing getattr

I´m want to display dinamically a table in a html template. In the view I have: def get_object( request ): objects = MyObject.objects.all() return render(request, 'table.html', { 'myObjects':objects, 'fields':ObjectAdmin.list_display}) then…
Diego Quirós
  • 898
  • 1
  • 14
  • 28
2
votes
0 answers

How to have a lambda function evaluate a variable now (and not postponed)

I have a class for a hardware object (here Fridge), and I'd like to automatically create HTTP API routes for a given subset of Fridge's methods, here open and close. from bottle import Bottle, request class Fridge: def _not_exposed(self): …
Basj
  • 41,386
  • 99
  • 383
  • 673
2
votes
4 answers

Python multilevel getattr with string

I have a function (here called analyze_data) that takes an object and a string as input, and uses the string to "extract" data via getattr. This works fine when the data are in the "frist" level of attributes (data1). However, I don't know what I…
Dave
  • 171
  • 2
  • 13
2
votes
1 answer

Using getattr() to access built-in functions

I would like to use getattr() to access Python's built-in functions. Is that possible? For example: getattr(???, 'abs') I know I can just simply do: >>> abs But I want to use getattr, because the keyword names are strings.
Phylosopher
  • 61
  • 1
  • 4
2
votes
1 answer

Why is a "rogue" shape attribute appearing in a Python class instance when __getattribute__ and __getattr__ are present?

I am trying to understand the function of the dunder getattribute and getattr methods. While experimenting, I noticed an unexpected shape attribute showing up in my class. I can't find any explanation for why this is happening. class X: def…
swright573
  • 41
  • 2
2
votes
1 answer

Module does not define class error while replacing default admin site

I'm following a tutorial for the Django admin app and I'm trying a step in which the default admin site is replaced with a custom subclass of AdminSite from django.contrib.admin. My project structure is: MyAdminTutorial |- MyAdminTutorial |-…
maja
  • 697
  • 5
  • 18
2
votes
1 answer

Can't parse bs4 src attribute using the getattr() function

I've created a script to parse two fields from every movie container from a webpage. The script is doing fine. I'm trying to use this getattr() function to scrape text and src from two fields, as in movie_name and image_link. In case of movie_name,…
MITHU
  • 113
  • 3
  • 12
  • 41
2
votes
1 answer

XML Parsing to get Attribute Value

I am parsing a xml using SAX Parser. Everythings working fine when the data I need to get is the body of a xml tag. The only problem I am getting is when the data I need is the attribute value of that XML tag. How do i get this attribute value?
Gaurav
  • 1,700
  • 4
  • 22
  • 38
2
votes
1 answer

Get type object defined inside doctest by type name

I am trying to doc-test a method that accepts a module object module and a string with the name of the type type_name inside that module : def get_type_from_string(module, type_name): """ >>> class A: pass >>> A.__module__ …
Hlib Babii
  • 599
  • 1
  • 7
  • 24
2
votes
3 answers

Using __setattr__ and __getattr__ for delegation with __slots__ without triggering infinite recursion

class A: __slots__ = ("a",) def __init__(self) -> None: self.a = 1 class B1: __slots__ = ("b",) def __init__(self, b) -> None: self.b = b def __getattr__(self, k): return getattr(self.b, k) def…
KOLANICH
  • 2,904
  • 2
  • 20
  • 20
2
votes
1 answer

Python " AttributeError: 'NoneType' object has no attribute " suddenly appearing

so i have multiple 5 'Player' objects being created upon the start of my code, and about 24 different 'Skill' objects for each 'Player' Object, im using this to loop through my code first_run = True while True: for i in…
m e m e
  • 105
  • 2
  • 11
2
votes
1 answer

python __getattribute__ RecursionError when returning class variable attribute

Why does Foo2 result in infinite recursion calling getattr of a class variable in __getattribute__, but Foo works fine making the same call in __getattr__? Any suggestions on how to get Foo2 to work? class Foobar(object): def __init__(self): …
kcb
  • 25
  • 6
2
votes
0 answers

How to save Pytorch tensor attributes to disk?

I used setattr to save some information with a Pytorch Tensor, which can be retrieved as expected: import torch before_write = torch.Tensor() setattr(before_write, "features", "my_features") print(before_write.features) >…
MartijnVanAttekum
  • 1,405
  • 12
  • 20
2
votes
1 answer

python __getattr__ doesn't work for built-in functions

I'm trying to build some kind of generic wrapper class to hold variable objects. class Var: def __init__(self, obj=None): self.obj=obj def __getattr__(self, attr): return getattr(self.obj, attr) Now for example, when I try…
yonayaha
  • 47
  • 7
2
votes
3 answers

How to write getatrribute so that variables in dict can be returned?

I have a class, and there is a dict in it. I want to visit variables in the dict by direct "instance.key" instead of "instance.d[key]". How to do that? class A: def __init__(self): self.a = 1 self.b = 2 self.fields =…
Linghao.Chen
  • 103
  • 1
  • 1
  • 5