Garfield Tong
Garfield Tong

Reputation: 420

Passing arbitrary amount of paramenters

Right now I am trying to do this:

def Setup(Param1) :
    repeat(Param1=Param1)

def perform(**kargs) :
    Param1.func()

class Param1 :
    def func(Self) :
        Do_Stuff_To_Self
        return Self

I am not sure I properly representing my code, but it's such a large convoluted project that I feel like posting the entire thing would make this worse, but essentially I am working on a wrapper of sorts (perform) that will perform code that is similar between each individual "func" but when I made it and tried building, I am getting the error:

global name 'Param1' is not defined.

I used the debugger and found that kargs is properly storing 'Param1': "... at instance 0x..." so I am not sure why it is saying Param1 is not defined?

Any help would be appreciated.

Thank you,

Weilun

Upvotes: 0

Views: 49

Answers (1)

koralarts
koralarts

Reputation: 2112

Passing arbitrary named parameters to a function in Python will store it inside kwargs which is a dictionary.

Example:

def do_stuff(**kwargs):
  kwargs.get('param1')  # would be 'param1'
  kwargs.get('param2')  # would be 5

do_stuff(param1='param1', param2=5)

You can also use unnamed parameters where the parameters will be stored as a list

def do_stuff2(*args):
  args[0]  # would be 'param1'
  args[1]  # would be 5

do_stuff2('param1', 5)

Upvotes: 1

Related Questions