0

I wanted to use overloading in Iron Python but seems it's not working :

import sys
import clr

def  af(a, b):
     c = a+b
     print c
     return c
def af(j):
  y = j*j
  print y
  return y

 af(6,7)
 af(5)

I get a error =\ Is there any way to use overloading ? my purpose is to write a function : foo(doAction,numTimes) when by default if I use foo(action): it will do it once, or I'll write : foo(action,6)

thanks a lot!!!

2

1 Answer 1

1

IronPython might run on the CLR but that doesn't make it C#. In any kind of Python, you can only define a function once. Defining a function is really just assigning to a name, so in your code you assign a function to af, then assign another one to the same name, so the first one is simply discarded.

The way to do this in Python is via default arguments:

def aj(a, b=None):
    if b is not None:
        result = a + b
    else:
        result = a * a
    print result
    return result

For your actual use case of course you can define numtimes with a default of 1:

def foo(action, numtimes=1):
    # whatever
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.