Setting local variable of a global function via kwargs in Python -


not being able overload functions in python brought me *args , **kwargs pass undefined amounts of arguments , variable definitions called function. tried apply concept simple function prints arguments screen using seperator (and without nasty "" around while). can find code snippet @ end of post.

my problem don't know correct way assign values of kwargs respective parameters outside classes. if want use setattr takes in 3 arguments (setatt(self, key, value)) will, of course, give me error when try pass key , value. there proper way assign values parameters global functions outside classes?

i hope can clear skies me.

here not working code example wanna work finding alternative setattr(self, key, value):

import sys  def printall(*args, **kwargs):     sep = ""     key, value in kwargs.items():         setattr(key, value)     arg in args:         sys.stdout.write(str(arg))         sys.stdout.write(sep)  nr = 9876543210 printall("how many", "arguments", " want", nr, sep='-') 

edit: ok, here updated , working example function after reading answers of @martijn pieters , @fabian:

def printall(*args, **kwargs):     # use sep = 'value' define seperator or use default 1     sep = kwargs.get('sep')     if sep none:         sep = ""     arg in args:         sys.stdout.write(str(arg))         sys.stdout.write(sep) 

you don't set kwargs values local variables. it's dictionary, refer values contained:

sys.stdout.write(kwargs.get('sep', '')) 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -