introspection

Passing a python class constant to a decorator without self

Passing a python class constant to a decorator without self Question: I’m using the python ratelimit library and it uses a decorator to handle rate limiting of a function. I have a class constant I’d like to pass into the decorator but of course self wont work. Is there a way to reference the UNIVERSALIS_MAX_CALLS_PER_SECOND …

Total answers: 2

Python: how to get size of all objects in current namespace?

Python: how to get size of all objects in current namespace? Question: I have some code that I am running from my own package and the program is using a lot more memory (60GB) than it should be. How can I print the size of all objects (in bytes) in the current namespace in order …

Total answers: 3

How to test if an object is a function vs. an unbound method?

How to test if an object is a function vs. an unbound method? Question: def is_unbound_method(func): pass def foo(): pass class MyClass(object): def bar(self): pass What can I put in the body of is_unbound_method so that is_unbound_method(foo) == False is_unbound_method(MyClass().bar) == False is_unbound_method(MyClass.bar) == True ?? Asked By: Dan Passaro || Source Answers: An unbound …

Total answers: 4

What does the slash mean in help() output?

What does the slash mean when help() is listing method signatures? Question: What does the / mean in Python 3.4’s help output for range before the closing parenthesis? >>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return a …

Total answers: 3

How to list all fields of a class (and no methods)?

How to list all fields of a class (and no methods)? Question: Suppose o is a Python object, and I want all of the fields of o, without any methods or __stuff__. How can this be done? I’ve tried things like: [f for f in dir(o) if not callable(f)] [f for f in dir(o) if …

Total answers: 6

Python inspect.stack is slow

Python inspect.stack is slow Question: I was just profiling my Python program to see why it seemed to be rather slow. I discovered that the majority of its running time was spent in the inspect.stack() method (for outputting debug messages with modules and line numbers), at 0.005 seconds per call. This seems rather high; is …

Total answers: 3

What is the correct way to override the __dir__ method?

What is the correct way to override the __dir__ method? Question: This question is meant to be more about __dir__ than about numpy. I have a subclass of numpy.recarray (in python 2.7, numpy 1.6.2), and I noticed recarray‘s field names are not listed when diring the object (and therefore ipython’s autocomplete doesn’t work). Trying to …

Total answers: 3

What's the biggest difference between dir() and __dict__ in Python

What's the biggest difference between dir() and __dict__ in Python Question: class C(object): def f(self): print self.__dict__ print dir(self) c = C() c.f() output: {} [‘__class__’, ‘__delattr__’,’f’,….] why there is not a ‘f’ in self.__dict__ Asked By: shellfly || Source Answers: dir() does much more than look up __dict__ First of all, dir() is a …

Total answers: 3

Introspect calling object

Introspect calling object Question: How do I introspect A‘s instance from within b.func() (i.e. A‘s instance’s self): class A(): def go(self): b=B() b.func() class B(): def func(self): # Introspect to find the calling A instance here Asked By: Jonathan Livni || Source Answers: You pass it to b.func() as an argument. Answered By: Benjamin Peterson …

Total answers: 4

Get all object attributes in Python?

Get all object attributes in Python? Question: Is there a way to get all attributes/methods/fields/etc. of an object in Python? vars() is close to what I want, but it doesn’t work unless an object has a __dict__, which isn’t always true (e.g. it’s not true for a list, a dict, etc.). Asked By: user541686 || …

Total answers: 4