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

getattr to sort or pop from string

I'm a bit confused about using getattr() with a string. I can do something like list_ = [] getattr(list_, 'insert')(0,1) And that will work as expected. When I try to do getattr(list_, 'sort') or getattr(list_, 'pop') then it does not work as…
TheStrangeQuark
  • 2,257
  • 5
  • 31
  • 58
0
votes
2 answers

When any why is it useful to have a descriptor's `__get__` method return another descriptor?

I had a container holding an object's methods, and possibly some descriptors. I wanted to test whether the descriptors had already been unwrapped or not by checking whether they had had a 'get' method. To my surprise, the __get__ method of a…
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
0
votes
3 answers

Python - Transform class attributes at call time

Suppose I have the following class class Headings: standard_heading = { 'height': 3.72, 'width': 25.68, 'left': 1.65, 'top': 0.28 } As an example, I want the following results where all values have been…
Ludo
  • 2,307
  • 2
  • 27
  • 58
0
votes
1 answer

Using getattr to call a function in a separate class

I may be trying to do something that is outside of the realm of possibility here, but I figured I would ask first before abandoning hope. So here it goes... I have 2 classes, A and B. Each class has an arbitrary number of functions. Class B will be…
0
votes
1 answer

Can't read entry value from one class's instance to another in Tkinter while reading from the same class's init works fine

I have two classes in my project. One is the GUI and the other makes the calculations. I read data from two Entry boxes from the first and want to pass them to the second. So far, what I do is this: class OptimizationGUI: land_entry = 0 …
tzoukritzou
  • 337
  • 1
  • 4
  • 16
0
votes
0 answers

How can we get the attributes of an wrapped object, obj, when the wrapper __getattribute__ calls obj's __getitem__ method?

Suppose DotDict is a wrapper class which allows you to access the wrapped object's __getitem__ method through __getattr__ Suppose obj is not an instance of DotDict, and the following is true about obj: obj.a returns 'apple' obj[a] returns 999 Now…
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
0
votes
2 answers

Call method from string inside class?

I am trying to call fucntions using string value. Here is a simple example of my problem. How to call method(line) properly? I tried different solutions and got success only with @staticmethod but this is not what I want. class A(): def…
Gatto Nou
  • 89
  • 1
  • 12
0
votes
1 answer

Why can't multiprocess.Process call getattr method?

Trying to call two methods say_hello and say_world by getattr() in multiprocessing.Process, but method say_world hasn't been executed. How can I make it possible? Thanks. # -*- coding: utf-8 -*- from multiprocessing import Process import time class…
zzxwill
  • 536
  • 2
  • 6
  • 16
0
votes
1 answer

extending a class that uses __getattr__ (pygame.Rect)

I am trying to create my own version of a pygame.Rect rectangle, but with the added feature that when a square is out of certain worldbounds, it appears on the other side. This means I had to rewrite a lot of functions of pygame.Rect in my…
0
votes
2 answers

Python __getattr__ behavior? IN ECLIPSE/PyDev console?

The following: class A(object): def __getattr__(self, attr): try: return self.__dict__[attr] except KeyError: self.__dict__[attr] = 'Attribute set to string' print 'Assigned attribute' …
Izz ad-Din Ruhulessin
  • 5,955
  • 7
  • 28
  • 28
0
votes
0 answers

Is it possible to determine in __getattr__ whether an attribute or method is accessed?

I'm aware that methods are just objects that can be accessed via getattr(obj, 'method_name'). Is the method does not exist, this will trigger obj.__getattr__(method_name). However, is it possible in the __getattr__ implementation to distinct whether…
danijar
  • 32,406
  • 45
  • 166
  • 297
0
votes
1 answer

Linking container class properties to contained class properties

I'm working on a simulation of a cluster of solar panels (a system/container). The properties of this cluster are linked almost one-to-one to the properties of the elements -- the panels (subsystem/contained) -- via the number of elements per…
balletpiraat
  • 206
  • 1
  • 11
0
votes
1 answer

How to return value with dynamic method calls through getattr()

I have a class that is called that runs a while loop command prompt, i am using dir() and getattr() to dynamically create a list of methods for a command shell. I want to return values, but return from a dynamically called method just exits to main…
0
votes
1 answer

Does__getattr__ allow modifying passed value?

This is my code: class myclass: def __init__(self): self.value = 5 def __getattr__(self, attrname): if attrname == 'value': return 10 X = myclass() print X.value I suppose the output value should be 10…
0
votes
1 answer

Can't parse string "N/A" from .XLSX in Python 2.7 with pandas

I've got a large Excel spreadsheet that I'm trying to parse into Python2.7 and pandas 0.19.1 to insert the data into a MySQL database. As part of this, some of the cells contain the string "N/A". Unfortunately, pandas doesn't seem to be able to…