What is a way to extract arguments from __init__
without creating new instance.
The code example:
class Super:
def __init__(self, name):
self.name = name
I am looking something like Super.__dict__.keys()
type solution. Just to retrieve name argument information without adding any values. Is there such an option to do that?
Update for Python 3.3+ (as pointed out by beeb in the comments)
You can use inspect.signature
introduced in Python 3.3:
class Super:
def __init__(self, name, kwarg='default'):
print('instantiated')
self.name = name
>>> import inspect
>>> inspect.signature(Super.__init__)
<Signature (self, name, kwarg='default')>
Original answer below
You can use inspect
>>> import inspect
>>> inspect.getargspec(Super.__init__)
ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=None)
>>>
Edit: inspect.getargspec
doesn't actually create an instance of Super
, see below:
import inspect
class Super:
def __init__(self, name):
print 'instantiated'
self.name = name
print inspect.getargspec(Super.__init__)
This outputs:
### Run test.a ###
ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=None)
>>>
Note that instantiated
never got printed.