metaprogramming

Get locals from calling namespace in Python

Get locals from calling namespace in Python Question: I want to retrieve the local variables from Python from a called function. Is there any way to do this? I realize this isn’t right for most programming, but I am basically building a debugger. For example: def show_locals(): # put something in here that shows local_1. …

Total answers: 3

Accessing function as attribute in a Python class

Accessing function as attribute in a Python class Question: I’m in a situation where it would be extremely useful (though not strictly necessary) to access a class’ instancemethod as an attribute. (it’s for an API that uses getattr to set some return values for a dictionary and I don’t want to mess the neat little …

Total answers: 2

What is the __dict__.__dict__ attribute of a Python class?

What is the __dict__.__dict__ attribute of a Python class? Question: >>> class A(object): pass … >>> A.__dict__ <dictproxy object at 0x173ef30> >>> A.__dict__.__dict__ Traceback (most recent call last): File “<string>”, line 1, in <fragment> AttributeError: ‘dictproxy’ object has no attribute ‘__dict__’ >>> A.__dict__.copy() {‘__dict__’: <attribute ‘__dict__’ of ‘A’ objects> … } >>> A.__dict__[‘__dict__’] <attribute ‘__dict__’ …

Total answers: 3

Modify default queryset in django

Modify default queryset in django Question: I have added a ‘cancelled’ field to my model, is there a way to modify the model default query to something like cancelled=False ? without having to modify all my filter/exclude queries ? Asked By: juanefren || Source Answers: You can do this with a custom model manager and …

Total answers: 2

How to find out the arity of a method in Python

How to find out the arity of a method in Python Question: I’d like to find out the arity of a method in Python (the number of parameters that it receives). Right now I’m doing this: def arity(obj, method): return getattr(obj.__class__, method).func_code.co_argcount – 1 # remove self class Foo: def bar(self, bla): pass arity(Foo(), “bar”) …

Total answers: 5

Dynamic/runtime method creation (code generation) in Python

Dynamic/runtime method creation (code generation) in Python Question: I need to generate code for a method at runtime. It’s important to be able to run arbitrary code and have a docstring. I came up with a solution combining exec and setattr, here’s a dummy example: class Viking(object): def __init__(self): code = ”’ def dynamo(self, arg): …

Total answers: 6

Python decorator makes function forget that it belongs to a class

Python decorator makes function forget that it belongs to a class Question: I am trying to write a decorator to do logging: def logger(myFunc): def new(*args, **keyargs): print ‘Entering %s.%s’ % (myFunc.im_class.__name__, myFunc.__name__) return myFunc(*args, **keyargs) return new class C(object): @logger def f(): pass C().f() I would like this to print: Entering C.f but instead …

Total answers: 9

Python vs. Ruby for metaprogramming

Python vs. Ruby for metaprogramming Question: I’m currently primarily a D programmer and am looking to add another language to my toolbox, preferably one that supports the metaprogramming hacks that just can’t be done in a statically compiled language like D. I’ve read up on Lisp a little and I would love to find a …

Total answers: 31

Python dictionary from an object's fields

Python dictionary from an object's fields Question: Do you know if there is a built-in function to build a dictionary from an arbitrary object? I’d like to do something like this: >>> class Foo: … bar = ‘hello’ … baz = ‘world’ … >>> f = Foo() >>> props(f) { ‘bar’ : ‘hello’, ‘baz’ : …

Total answers: 20