Finding a function's parameters in Python

后端 未结 1 2040
春和景丽
春和景丽 2020-12-30 17:17

I want to be able to ask a class\'s __init__ method what it\'s parameters are. The straightforward approach is the following:

cls.__init__.__fu         


        
相关标签:
1条回答
  • 2020-12-30 17:50

    Consider this decorator:

    def rickroll(old_function):
        return lambda junk, junk1, junk2: "Never Going To Give You Up"
    
    class Foo(object):
        @rickroll
        def bar(self, p1, p2):
            return p1 * p2
    
    print Foo().bar(1, 2)
    

    In it, the rickroll decorator takes the bar method, discards it, replaces it with a new function that ignores its differently-named (and possibly numbered!) parameters and instead returns a line from a classic song.

    There are no further references to the original function, and the garbage collector can come and remove it any time it likes.

    In such a case, I cannot see how you could find the parameter names p1 and p2. In my understanding, even the Python interpreter itself has no idea what they used to be called.

    0 讨论(0)
提交回复
热议问题