python-datamodel

Is everything greater than None?

Is everything greater than None? Question: Is there a Python built-in datatype, besides None, for which: >>> not foo > None True where foo is a value of that type? How about Python 3? Asked By: Attila O. || Source Answers: None is always less than any datatype in Python 2 (see object.c). In Python …

Total answers: 2

Get fully qualified class name of an object in Python

Get fully qualified class name of an object in Python Question: For logging purposes I want to retrieve the fully qualified class name of a Python object. (With fully qualified I mean the class name including the package and module name.) I know about x.__class__.__name__, but is there a simple method to get the package …

Total answers: 13

Get class that defined method

Get class that defined method Question: How can I get the class that defined a method in Python? I’d want the following example to print “__main__.FooClass“: class FooClass: def foo_method(self): print “foo” class BarClass(FooClass): pass bar = BarClass() print get_class_that_defined_method(bar.foo_method) Asked By: Jesse Aldridge || Source Answers: import inspect def get_class_that_defined_method(meth): for cls in inspect.getmro(meth.im_class): …

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

What are metaclasses in Python?

What are metaclasses in Python? Question: What are metaclasses? What are they used for? Asked By: e-satis || Source Answers: Note, this answer is for Python 2.x as it was written in 2008, metaclasses are slightly different in 3.x. Metaclasses are the secret sauce that make ‘class’ work. The default metaclass for a new style …

Total answers: 25

How do you check whether a python method is bound or not?

How do you check whether a python method is bound or not? Question: Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it’s bound to? Asked By: readonly || Source Answers: im_self attribute (only Python …

Total answers: 5