3

I've got the following function:

def create(self, name, page=5, opt1=False, opt2=False,
                        opt3=False,
                        opt4=False,
                        opt5=False,
                        opt6=False,
                        *parameters):

Is it possible to assign only one of the optional parameters, and some *parameters? e.g.

create('some name', opt4=True, 1, 2, 3) # I need 1, 2, 3 to be assigned to *parameters

Most of the time, I don't need to change the values of opt1...opt6, I only need to change maybe one of them, and assign some other *parameters. So I am looking for a way to avoid setting opt1...opt6 if I don't want to change their default value.

3
  • how will use use 1 2 and 3, how is a user to know what they do? Commented Jul 11, 2015 at 10:53
  • @PadraicCunningham They could be any arbitrary list of numbers. Not necessarily 1, 2, 3. They could be 34, 100, 12, 18. Commented Jul 11, 2015 at 11:00
  • what I meant was the optional *parameters is going to be treated as an iterable in your code so you could make another keyword arg nums=None and take an iterable as an argument Commented Jul 11, 2015 at 11:08

2 Answers 2

1

Well for sure this is not nice, but you can probably work on variants of this

def create(name, page=5, opt1=False, opt2=False,
           opt3=False,
           opt4=False,
           opt5=False,
           opt6=False,
           *parameters):
    print(parameters)
    print(page)
    print(opt4)

myArgs = dict(zip(['page','opt1','opt2','opt3','opt4','opt5','opt6'],create.func_defaults))

myArgs['opt4']=True
create("MyName",**myArgs)
Sign up to request clarification or add additional context in comments.

Comments

0

In Python 3.x, Try putting parameters before any of the default arguments, example -

def create(self, name , *parameters, page=5, opt1=False, 
                                  opt2=False,opt3=False,
                                  opt4=False,opt5=False,
                                  opt6=False):
    print(parameters)
    print(page)
    print(opt4)

In [31]: create('C', 'some name', 1, 2, 3, opt4 = True)
(1, 2, 3)
5
True

Please understand that with this method, you can only call the arguments with default value using named arguments.


For Python 2.x , you can try using *parameters and then **kwargs for the default parameters -

>>> def create(self, name, *parameters, **kwargs):
...     kwargs.setdefault('opt4',False)  #You will need to do for all such parameters.
...     print(parameters)
...     print(kwargs['opt4'])
... 
>>> create('C', 'some name', 1, 2, 3, opt4 = True)
(1, 2, 3)
True

Again, only way to set the values for opt1...opt6 or page , etc would be using named arguments.

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.