[Python] Get arguments that an object's __init__ was called with

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    Is there a way to get an object's init argument values in python 2.7? I'm able to get the defaults through getargspec but i would like to access passed in values

    import inspect

    class AnObject(object):
    def __init__(self, kw='', *args, **kwargs):
    print 'Hello'

    anobj = AnObject(kw='a keyword arg')
    print inspect.getargspec(anobj.__init__)


    Returns

    Hello
    ArgSpec(args=['self', 'kw'], varargs='args', keywords='kwargs', defaults=('',))

    Continue reading...

Compartilhe esta Página