getattr Questions

1

for example i use this code: class A(object): def __init__(self): self.dict1 = { 'A': 3, 'B': self.A} def __getattr__(self, key): if key in self.dict1: return self.dict1[key] a = A() and ...
Monastery asked 16/1, 2016 at 15:45

2

Solved

I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self)...
Harlanharland asked 28/9, 2015 at 17:7

3

Solved

How do I override the __getattr__ method of a class without breaking the default behavior?
Sanguine asked 8/3, 2010 at 23:29

2

I have a class that implements virtual attributes using __getattr__. The attributes can be expensive, e.g. performing a query. Now, I am using a library which checks if my object has the attribute ...
Willi asked 17/5, 2015 at 18:15

1

Solved

Subj. Example: MyList(object): def __init__(self, list_to_be_wrapped): self._items = list_to_be_wrapped def __getattr__(self, item): return getattr(self._items, item) Then MyList([1, 2...
Kirmess asked 13/3, 2015 at 23:56

6

Solved

I want to override access to one variable in a class, but return all others normally. How do I accomplish this with __getattribute__? I tried the following (which should also illustrate what I'm t...
Collective asked 16/12, 2008 at 16:4

2

Solved

This one has me baffled. Consider the following Django models - representing zookeepers and the cages at the zoo that they are responsible for cleaning: class Zookeeper(moodels.Model): name = mod...
Inquisitionist asked 17/7, 2014 at 4:2

1

Solved

Given: In [37]: class A: ....: f = 1 ....: In [38]: class B(A): ....: pass ....: In [39]: getattr(B, 'f') Out[39]: 1 Okay, that either calls super or crawls the mro? In [40]: getattr(A, '...
Cipolin asked 10/6, 2014 at 21:10

1

Solved

I have this function call after importing foo.py. Foo has several methods that I need to call e.g. foo.paint, foo.draw: import foo code if foo: getattr(foo, 'paint')() I need to use a w...
Bureaucracy asked 19/2, 2014 at 16:1

3

Solved

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 cas...
Octopod asked 28/8, 2013 at 21:14

2

I'm working with somethign similar to this code: class BaseClass(object): def __getattr__(self, attr): return lambda:'1' class SubClass(BaseClass): def foo(self): suffix = '2' return super(S...
Nonparticipation asked 23/8, 2013 at 23:22

3

Solved

When an attribute is not found object.__getattr__ is called. Is there an equivalent way to intercept undefined methods?
Priapic asked 4/10, 2010 at 12:2

2

Solved

I want to write a wrapper class which takes a value and behaves just like it except for adding a 'reason' attribute. I had something like this in mind: class ExplainedValue(object): def __init__(...
Sharilyn asked 24/1, 2013 at 23:16

1

Solved

I can dynamically look up object attribute values using object.__dict__[some_key] or getattr(object, some_key). Which is faster or better, and why? >>> class SomeObject: ... pass ... >...
Wardroom asked 29/12, 2012 at 18:56

1

setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self taught). Considering that exec and eval inherit a potential danger since in s...
Hobby asked 20/12, 2012 at 23:47

5

Solved

I have and object with an pseudo or special attribute that can be named in three different ways (Note: I don't control the code which generates the object) The value in the attributes (depen...
Hepatic asked 28/11, 2012 at 0:23

1

Solved

I have one object wrapped inside another. The "Wrapper" accesses the attributes from the "Wrapped" object by overriding __getattr__. This works well until I need to override an atribute on a sub cl...
Kittie asked 21/8, 2012 at 2:50

3

Solved

I would like to modify an object private variable class Example(): __myTest1 = 1 __myTest2 = 1 def __init__(self): pass def modifyTest(self, name = 'Test1', value): setattr(self, '__my'+name...
Macaluso asked 3/4, 2012 at 9:21

1

Solved

The example below is from a REST database driver on Python 2.7. In the __setattr__ method below, if I use the commented out getattr() line, it reduces the object instantiation performance from 600...
Rumanian asked 20/3, 2012 at 16:35

4

I want to dynamically query which objects from a class I would like to retrieve. getattr seems like what I want, and it performs fine for top-level objects in the class. However, I'd like to also s...
Hydro asked 1/2, 2012 at 18:19

2

Solved

I have to customize __getattr__ to call another function to read. This works well except the help(object.attr) does not work. This code is used in an interactive environment, so the help() becomes...
Templet asked 3/2, 2012 at 16:11

4

I am trying to create a subclass which acts as a list of custom classes. However, I want the list to inherit the methods and attributes of the parent class and return a sum of the quantities of eac...
Ribeiro asked 30/8, 2011 at 18:19

3

Trying to access/assign items in a list with getattr and setattr funcions in Python. Unfortunately there seems to be no way of passing the place in the list index along with the list name. Here's s...
Wilmot asked 1/8, 2011 at 3:10

3

Solved

I have methods that accept dicts or other objects and the names of "fields" to fetch from those objects. If the object is a dict then the method uses __getitem__ to retrieve the named key, or else ...
Lao asked 18/7, 2011 at 19:14

1

Solved

What is the best approach to take if you want to dynamically create and reference nested attributes? I was writing a simple Flickr client, and wanted to match the documented API as closely as poss...
Palstave asked 27/6, 2011 at 20:47

© 2022 - 2024 — McMap. All rights reserved.