Linked Questions

4 votes
1 answer
3k views

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]...
Manuel Ebert's user avatar
  • 8,559
0 votes
2 answers
7k views

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 ...
Mokus's user avatar
  • 10.5k
2 votes
0 answers
101 views

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, **...
emihir0's user avatar
  • 1,270
0 votes
0 answers
51 views

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':...
yoni keren's user avatar
690 votes
46 answers
514k views

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':...
Marc's user avatar
  • 7,031
140 votes
8 answers
45k views

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 ...
Josh Gibson's user avatar
  • 23.3k
151 votes
6 answers
141k views

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 ...
Greg's user avatar
  • 47.5k
19 votes
9 answers
39k views

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 ...
Stefano Borini's user avatar
13 votes
8 answers
9k views

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="...
kjfletch's user avatar
  • 5,514
22 votes
2 answers
16k views

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 ...
rz.'s user avatar
  • 20.1k
10 votes
4 answers
8k views

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 >&...
Ceasar's user avatar
  • 23.2k
10 votes
2 answers
3k views

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# ...
Olaf's user avatar
  • 371
4 votes
2 answers
7k views

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 ...
Ben's user avatar
  • 353
3 votes
3 answers
2k views

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 = ...
MRocklin's user avatar
  • 57.5k
3 votes
4 answers
2k views

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 ...
Priyatham's user avatar
  • 2,937

15 30 50 per page