Search code examples
pythonpython-3.xfunctionpythonista

Return a function with parameters


I am trying to make a function that returns itself with parameters.

Def a (x,y):
   *code*
   Return a(x,y)

this returns an error when I try to run it.

>>> a(1,2)
RecursionError: maximum recursion depth exceeded

What I want is

>>> a(1,2)
a(1,2)

It there a way to return the function with parameters?

I know it can be done but I don’t know how to do it

>>> datetime.time(0,0)
Datetime.time(0,0)

Edit: Preferably I would like not to have to import any modules


Solution

  • I think you've misunderstood what is "returned" when you call the following row:

    >>> datetime.time(0,0)
    Datetime.time(0,0)
    

    The original call datetime.time(0,0) creates a datatime-object to the console. Because you're not saving the returned object, the console automatically calls for the class-method called __repr__.

    To replicate the behavior we can create the following class:

    >>> class a:
    ...   def __init__(self, x,y):
    ...     self.x = x
    ...     self.y = y
    ...   def __repr__(self):
    ...     return "a({},{})".format(self.x, self.y)
    ...
    >>> a(1,2)
    a(1,2)