python-datamodel

Getting method parameter names in Python

China☆狼群 提交于 2019-11-25 23:48:35
问题 Given the Python function: def a_method(arg1, arg2): pass How can I extract the number and names of the arguments. I.e., given that I have a reference to func , I want the func.[something] to return (\"arg1\", \"arg2\") . The usage scenario for this is that I have a decorator, and I wish to use the method arguments in the same order that they appear for the actual function as a key. I.e., how would the decorator look that printed \"a,b\" when I call a_method(\"a\", \"b\") ? 回答1: Take a look

Getting the class name of an instance?

此生再无相见时 提交于 2019-11-25 23:07:43
How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived? Was thinking maybe the inspect module might have helped me out here, but it doesn't seem to give me what I want. And short of parsing the __class__ member, I'm not sure how to get at this information. sykora Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of the class, which I think is what you want. >>> import itertools >>> x = itertools.count(0) >>> type(x)._

What are metaclasses in Python?

隐身守侯 提交于 2019-11-25 22:12:30
问题 What are metaclasses and what do we use them for? 回答1: A metaclass is the class of a class. A class defines how an instance of the class (i.e. an object) behaves while a metaclass defines how a class behaves. A class is an instance of a metaclass. While in Python you can use arbitrary callables for metaclasses (like Jerub shows), the better approach is to make it an actual class itself. type is the usual metaclass in Python. type is itself a class, and it is its own type. You won't be able to