reflection

How to inspect return type of Callable

How to inspect return type of Callable Question: Let’s say I have something like the following: import inspect from collections.abc import Callable # Using Python 3.10+ from typing import get_type_hints def foo(arg: Callable[…, int]) -> None: pass type_hints = get_type_hints(foo)["arg"] annotation = inspect.signature(foo).parameters["arg"].annotation # How can one get the return type `int` from either of …

Total answers: 2

Can a line of Python code know its indentation nesting level?

Can a line of Python code know its indentation nesting level? Question: From something like this: print(get_indentation_level()) print(get_indentation_level()) print(get_indentation_level()) I would like to get something like this: 1 2 3 Can the code read itself in this way? All I want is the output from the more nested parts of the code to be more …

Total answers: 5

How can I return a default value for an attribute?

How can I return a default value for an attribute? Question: I have an object myobject, which might return None. If it returns None, it won’t return an attribute id: a = myobject.id So when myobject is None, the stament above results in a AttributeError: AttributeError: ‘NoneType’ object has no attribute ‘id’ If myobject is …

Total answers: 7

Convert a python 'type' object to a string

Convert a python 'type' object to a string Question: I’m wondering how to convert a python ‘type’ object into a string using python’s reflective capabilities. For example, I’d like to print the type of an object print("My type is " + type(some_object)) # (which obviously doesn’t work like this) Asked By: Rehno Lindeque || Source …

Total answers: 5

How to get (sub)class name from a static method in Python?

How to get (sub)class name from a static method in Python? Question: If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): # code pass Is it possible for a function call Foo.bar() to determine the class name Foo? Asked By: Jean-Pierre Chauvel || Source Answers: Replace the staticmethod with a classmethod. …

Total answers: 3

Checking if A is superclass of B in Python

Checking if A is superclass of B in Python Question: class p1(object): pass class p2(p1): pass So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ? Asked By: Andz || Source Answers: I think you meant to use “class” instead …

Total answers: 4

How can I get a list of all classes within current module in Python?

How can I get a list of all classes within current module in Python? Question: I’ve seen plenty of examples of people extracting all of the classes from a module, usually something like: # foo.py class Foo: pass # test.py import inspect import foo for name, obj in inspect.getmembers(foo): if inspect.isclass(obj): print obj Awesome. But …

Total answers: 13

How to enumerate an object's properties in Python?

How to enumerate an object's properties in Python? Question: I C# we do it through reflection. In Javascript it is simple as: for(var propertyName in objectName) var currentPropertyValue = objectName[propertyName]; How to do it in Python? Asked By: Jader Dias || Source Answers: for property, value in vars(theObject).items(): print(property, ":", value) Be aware that in …

Total answers: 8

Python: changing methods and attributes at runtime

Python: changing methods and attributes at runtime Question: I wish to create a class in Python that I can add and remove attributes and methods. How can I acomplish that? Oh, and please don’t ask why. Asked By: Migol || Source Answers: This example shows the differences between adding a method to a class and …

Total answers: 9