Linked Questions
44 questions linked to/from How to add property to a class dynamically?
4 votes
1 answer
3k views
Dynamically add properties to instances in Python [duplicate]
I've got essentially an elaborate wrapper around a list of dictionaries: class Wrapper(object): def __init__(self, data): self.data = data def get(self, attr): return [d[attr]...
0 votes
2 answers
7k views
Python dynamic class names [duplicate]
Possible Duplicate: Dynamic loading of python modules python: How to add property to a class dynamically? I have a dictionary with the filename and class names how can I import this class names ...
2 votes
0 answers
101 views
How to create dynamic property in __init__ [duplicate]
I have a simple scenario. I pass a kwarg prop_name and prop_func into my class init that I want to turn into property such as: class A: def __init__(self, *args, prop_name=None, prop_func=None, **...
0 votes
0 answers
51 views
How to define a few @property properties at the same time in python? [duplicate]
I have a dictionary in some class, self.tables={'tableA':df_a,'tableB':df_b,...,'tableZ':df_z} and a corresponding list of indices for each dataframe: self.indices={'tableA':indices_a,'tableB':...
690 votes
46 answers
514k views
How to convert a nested Python dict to object?
I'm searching for an elegant way to get data using attribute access on a dict with some nested dicts and lists (i.e. javascript-style object syntax). For example: >>> d = {'a': 1, 'b': {'c':...
140 votes
8 answers
45k views
Can modules have properties the same way that objects can?
With python properties, I can make it such that obj.y calls a function rather than just returning a value. Is there a way to do this with modules? I have a case where I want module.y to call a ...
151 votes
6 answers
141k views
How do I implement __getattribute__ without an infinite recursion error?
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 ...
19 votes
9 answers
39k views
struct objects in python
I wanted to create a throwaway "struct" object to keep various status flags. My first approach was this (javascript style) >>> status = object() >>> status.foo = 3 Traceback (most ...
13 votes
8 answers
9k views
Class factory to produce simple struct-like classes?
While investigating Ruby I came across this to create a simple Struct-like class: Person = Struct.new(:forname, :surname) person1 = Person.new('John', 'Doe') puts person1 #<struct Person forname="...
22 votes
2 answers
16k views
Dynamically adding @property in python
I know that I can dynamically add an instance method to an object by doing something like: import types def my_method(self): # logic of method # ... # instance is some instance of some class ...
10 votes
4 answers
8k views
How do I assign a property to an instance in Python?
Using python, one can set an attribute of a instance via either of the two methods below: >>> class Foo(object): pass >>> a = Foo() >>> a.x = 1 >>> a.x 1 >&...
10 votes
2 answers
3k views
Python property factory or descriptor class for wrapping an external library
I am writing a Python wrapper class for a C# API accessed through Pythonnet. As I want to extend the API with my own methods I decided to wrap it using the composition approach outlined here: The C# ...
4 votes
2 answers
7k views
Can I add attributes to class methods in Python?
I have a class like this: class A: def __init__(self): self.size=0 def change_size(self,new): self.size=new I want to add an attribute to the change_size method to say what it ...
3 votes
3 answers
2k views
Python class without attribute boilerplate
Many of my classes look like the following class to represent accounts class Account(object): def __init__(self, first, last, age, id, balance): self.first = first self.last = ...
3 votes
4 answers
2k views
How do I define setter, getter for dynamically added attributes
I have a class as follows: class A: def __init__(self): pass def add_attr(self, name): setattr(self, name, 'something') How do I define custom setter, getter for self.name? I ...