introspection

Get kwargs Inside Function

Get kwargs Inside Function Question: If I have a python function like so: def some_func(arg1, arg2, arg3=1, arg4=2): Is there a way to determine which arguments were passed by keyword from inside the function? EDIT For those asking why I need this, I have no real reason, it came up in a conversation and curiosity …

Total answers: 7

List all the modules that are part of a python package?

List all the modules that are part of a python package? Question: Is there a straightforward way to find all the modules that are part of a python package? I’ve found this old discussion, which is not really conclusive, but I’d love to have a definite answer before I roll out my own solution based …

Total answers: 6

How can I get the name of an object?

How can I get the name of an object? Question: Suppose I have code like: x = 0 y = 1 z = 2 my_list = [x, y, z] for item in my_list: print("handling object ", name(item)) # <— what would go instead of `name`? How can I get the name of each object in …

Total answers: 18

List all base classes in a hierarchy of given class?

List all base classes in a hierarchy of given class? Question: Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes – anywhere in the inheritance hierarchy – it issubclass of? Asked By: Sridhar Ratnakumar || Source Answers: you can use the __bases__ tuple of …

Total answers: 7

How can I list the methods in a Python 2.5 module?

How can I list the methods in a Python 2.5 module? Question: I’m trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can …

Total answers: 4

Get __name__ of calling function's module in Python

Get __name__ of calling function's module in Python Question: Suppose myapp/foo.py contains: def info(msg): caller_name = ???? print ‘[%s] %s’ % (caller_name, msg) And myapp/bar.py contains: import foo foo.info(‘Hello’) # => [myapp.bar] Hello I want caller_name to be set to the __name__ attribute of the calling functions’ module (which is ‘myapp.foo’) in this case. How …

Total answers: 4

How can you print a variable name in python?

How can you print a variable name in python? Question: Say I have a variable named choice it is equal to 2. How would I access the name of the variable? Something equivalent to In [53]: namestr(choice) Out[53]: ‘choice’ for use in making a dictionary. There’s a good way to do this and I’m just …

Total answers: 8

Getting the class name of an instance

Getting the class name of an instance Question: How do I find out the name of the class used to create an instance of an object in Python? I’m not sure if I should use the inspect module or parse the __class__ attribute. Asked By: Dan || Source Answers: type() ? >>> class A: … …

Total answers: 12

How to get method parameter names?

How to get method parameter names? Question: Given that a function a_method has been defined like def a_method(arg1, arg2): pass Starting from a_method itself, how can I get the argument names – for example, as a tuple of strings, like ("arg1", "arg2")? Asked By: Staale || Source Answers: In CPython, the number of arguments is …

Total answers: 20

Checking for member existence in Python

Checking for member existence in Python Question: I regularly want to check if an object has a member or not. An example is the creation of a singleton in a function. For that purpose, you can use hasattr like this: class Foo(object): @classmethod def singleton(self): if not hasattr(self, ‘instance’): self.instance = Foo() return self.instance But …

Total answers: 5