1

Is it possible to ignore passed/overgiven arguments in a method?

Here is an example from something I want to do:

def event_handler(one, two):
   print(one)
   print(two)

And at another place:

event_handler(one, two, three)

I mean that the third argument is optional. I've tried in Python Fiddle, but it doesn't work without an error.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Jonniboy
  • 136
  • 1
  • 13

2 Answers2

3

Make the third an default argument

def event_handler(one, two, third = None):

Then do error handling, but as it is default, it is easy.

def event_handler(one, two, third = None):
   print(one)
   print(two)
   if (three):
        print(three)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
2

Use variable arguments with *args.

def f(*args): pass

Access the vars as a list like args[0],... Or,

def f(required_arg1, required_arg2, *optional_args): pass
Nizam Mohamed
  • 8,751
  • 24
  • 32
  • the problem with this is it obscures the argument definition ... It is much better to use a default argument for this case imho ... – Joran Beasley Mar 28 '15 at 15:26