Python Forum
How to Print Function Prototype ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to Print Function Prototype ? (/thread-3276.html)



How to Print Function Prototype ? - harun2525 - May-10-2017

i want print function prototype. 

example
def foo(arg, kwarg = "py3.6", os = "Fedora"):
    pass

get_func_prototype(foo)

# output => '(arg, kwarg = "py3.6", os = "Fedora")'



RE: How to Print Function Prototype ? - micseydel - May-10-2017

How does this look? https://docs.python.org/3/library/inspect.html#inspect.getargspec


RE: How to Print Function Prototype ? - harun2525 - May-10-2017

Thanks.

sorry. I actually want to how to make same func prototype of other func prototype

example. (This is More important to me. I don't think there is no solution.)

def foo(script, lang = "Python", os = "Fedora"):
    pass

def bar( get_func_prototype(foo) ): # for same prototype with foo
    pass



RE: How to Print Function Prototype ? - micseydel - May-10-2017

That should be doable as well. Could you elaborate more on your use case? Like, are you just trying to wrap a function?


RE: How to Print Function Prototype ? - harun2525 - May-10-2017

class myd( dict ):
    def __init__( get_func_prototype(dict.__init__)  ): # for the same prototype because i don't know what dict.__init__ prototype is. and i don't want to rewrite same prototype.



RE: How to Print Function Prototype ? - micseydel - May-10-2017

You're still talking too much implementation rather than goal. Is your goal to generate at runtime a class which shares a signature with a function? Are you trying not to duplicate code at write-time? Or do you just want to support dynamic calls to the class? If you just need to support calls, you don't need to do anything fancy, just use *args and **kwdwargs. Also, is dict important in your example? I can't make sense of it.


RE: How to Print Function Prototype ? - harun2525 - May-10-2017

(May-10-2017, 08:10 PM)micseydel Wrote: You're still talking too much implementation rather than goal. Is your goal to generate at runtime a class which shares a signature with a function? Are you trying not to duplicate code at write-time? Or do you just want to support dynamic calls to the class? If you just need to support calls, you don't need to do anything fancy, just use *args and **kwdwargs. Also, is dict important in your example? I can't make sense of it.

sometimes we have to write same prototype many times. why does someone prefer long way rather than short way?

Edit:
Why do I use *args, **kwargs .Is this a right way for all funcs for all owerwriting ?


RE: How to Print Function Prototype ? - micseydel - May-10-2017

I'm not sure what you're writing that you're using the same function signature to the point where you don't want to keep writing it. I can't think of any solution, nor do I see this as a real problem. I don't think the *args or **kwargs will solve your problem (just shift the boiler plate).

I'd still like to see more context for what you're talking about. Like, if what you want those extra parameters for is just logging, then maybe a decorator would help.