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

What pandas DataFrame method tells ipython notebook to display as HTML

I have created a class in which the main deliverable piece of data is stored in an attribute as a pandas DataFrame. I'd like the default display behavior of instances of this class to be the same as that of this DataFrame. Particularly when in…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0
votes
2 answers

Python : getattr expected at least 2 arguments, got 1

I have got a beginner problem. I would like to get an attribute from an instance. It work fine when i use getattr with 2 separeted arguments (object and the name of the attribute). But I read that it's possible to just use one argument (like this :…
Morgan
  • 589
  • 9
  • 20
0
votes
1 answer

Getattr on self not working, is it supposed to work?

I have this very simple function inside an object: def ctrl_btn_press(self, event): print event.GetEventObject().GetName() getattr(self, event.GetEventObject().GetName()) Event is a wxpython Event. The names are in a list in a config file…
linus72982
  • 1,418
  • 2
  • 16
  • 31
0
votes
2 answers

why won't getattr work for built in attributes?

The following code should print "person is true" and "plant is true", but it only prints the first one. I've tested it, and for some reason my code only works for attributes that are set after the fact, not ones that are always true or false in the…
user3150635
  • 509
  • 2
  • 9
  • 26
0
votes
1 answer

PyQt/Pyside - Create & Connect Dynamically - capturing function and arguments in lambda

I'm trying to recreate the windows note pad. I'm currently messing with the QMenuBar I made a dictionary with all the menus and actions it will have in the following pattern: menus = {'File':[['New', 'Ctrl+n'], ['Open', 'Ctrl+o'], …
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
0
votes
1 answer

Python extension type: super() not finding method (a.k.a. attribute) in base class

I am recoding PyCXX which is a C++ wrapper for Python. The original (working) implementation for adding methods to a new style class involved creating an "extern C" handler function for each, populating a PyMethodDef table with pointers to these…
P i
  • 29,020
  • 36
  • 159
  • 267
0
votes
0 answers

getattr() can find some functions, but not others

As part of a learning project I'm writing a simple IRC bot and I've implemented simple text functions like !auth, !join, !say, etc. In order to make it easy to add functions I've implemented each in python as bot_auth(), bot_join(), bot_say(), etc,…
Sammitch
  • 30,782
  • 7
  • 50
  • 77
0
votes
1 answer

Using __getattr__ to call child methods for specific type?

I have a class who's fields are an instance of another class. class Field: def get(self): def set(self, value): def delete(self): class Document: def __init__(self): self.y = Field() self.z = True What I'd like to…
heiskr
  • 395
  • 2
  • 11
0
votes
0 answers

Finding attributes of a list with getattr

I have 2 modules. runtest.py, where main is defined and if name == "main": # general purpose library na = nalib.NALib() ... classnames = na.get_classnames(classes) Hence I'm getting all the classes preset in the module in…
NikAsawadekar
  • 57
  • 1
  • 5
0
votes
1 answer

Python AttributeError not catching

I have some code where i'm trying to catch an exception and stifle it, but no matter what I do the code never prints the statement in the except block. def check_configuration(cls, **kwargs): print 'product_config' errors = {} for arg in…
Tim
  • 2,134
  • 3
  • 26
  • 40
0
votes
2 answers

How can I use getattr to access an attribute within an attribute?

What I have is a value found in: value.number.odd = 7 number.odd is an input by the user, in x. and so if x='number.odd', I was hoping that: getattr(value, x) would display what value.number.odd would, but it doesn't. Instead I…
RRR
  • 69
  • 6
0
votes
1 answer

Use of __getattr__ in Python

How frequently should you use __getattr__ in Python, rather than normal methods? Which of these would be considered better? class A: def __getattr__(self, attribute): if attribute == "spam": return self._spam * 100 …
rlms
  • 10,650
  • 8
  • 44
  • 61
0
votes
1 answer

How to iterate through modules with user input

A user input module that iterates though imported modules as shown. How can i make it to iterate though all imported modules since it only iterates though foo only from packageA import foo # has open_book() function from packageB import bar# has…
user3346746
  • 327
  • 3
  • 14
0
votes
2 answers

Compare and grab items attributes

I have 3 locators in my scene, eg. Locator01 - localScaleY value of 1 Locator02 - localScaleY value of 2 Locator03 - localScaleY value of 3 Each with a varying value in its localScaleY. I had wanted to compare this 3 Locators' localScaleY values…
yan
  • 631
  • 9
  • 30
0
votes
3 answers

Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute?

Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute? Desired Examples: ns_attr_split("obj.attr") => ("obj", "attr") ns_attr_split("obj.arr[0]") => ("obj",…
Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62