简体   繁体   中英

Python: passing arguments to __init__

I am new to python and have the following init :

def __init__(self, name, iMD=False, iI=False, o=0, o1=0, o2=0,
                 t=QUIC, mF=QUIC_KW, pM=0, pD=0, nF=0, pA=0, iS=0, iPA=1, h='',
                 iRC='', sC='', iM=0, iS=0, pAIR=0, 
                 pAII=0, cM='', k=0.0, t=0.0, m=0.0, mi=0.0, av=0.0, mL=0.0,
                 mD=None, iD=None, cCI=None, cCSEI=None, rfI = 0)

Basically when I create this object I want to pass in all the arguments except for those that have a default value of None .

Is there a work around in Python? I also do not want to move the order of the arguments as there is a lot of legacy code.

EDIT: Just to clarify. Yes there are default values, however, I am creating this object with non-default values that is being read in from a database. Basically I need to pass in arguments for all parameters except for mD, iD, cCI and cCSEI.

I was hoping I could construct a new object like so: object(arg1, arg2, arg3, arg4, arg5, arg6, arg7, ... , arg27, , , , , arg28)

The only required argument for your method is name , and it must be the first argument. After that, all your arguments have default values, making them optional. Should you want to overwrite a default, only pass that value in:

foo = SomeClass('hello', rfI=10)

Also, may I suggest using meaningful names or at least adding a docstring to your method. You might also want to read the Python style guide .

I believe dictionary unpacking is what you're looking for:

class MyObj(object):
  def __init__(self, name, iMD=False, iI=False, o=0, o1=0, o2=0,
             t=QUIC, mF=QUIC_KW, pM=0, pD=0, nF=0, pA=0, iS=0, iPA=1, h='',
             iRC='', sC='', iM=0, iS=0, pAIR=0, 
             pAII=0, cM='', k=0.0, t=0.0, m=0.0, mi=0.0, av=0.0, mL=0.0,
             mD=None, iD=None, cCI=None, cCSEI=None, rfI = 0)

myArgs = {'rfI': 11}
m = MyObj('myname', **myArgs)
// m now has 'myname' as a name, rfI=11 and everything else as default

Note that this is exactly the same as saying:

m = MyObj('myname', rfI=11)

First Your Code has an Syntax Error: Duplicate Argument iS in function defination . I replaced one iS with iS2 . So now constructor is expecting maximum of 33 Arguments to initialize the class.

As you say you don't want to pass value to all except whose default value is None . Just simple pass 27 values to constructor

class foo():
    def __init__(self, name, iMD=False, iI=False, o=0, o1=0, o2=0,
                 t=QUIC, mF=QUIC_KW, pM=0, pD=0, nF=0, pA=0, iS=0, iPA=1, h='',
                 iRC='', sC='', iM=0, iS2=0, pAIR=0, 
                 pAII=0, cM='', k=0.0, t2=0.0, m=0.0, mi=0.0, av=0.0, mL=0.0,
                 mD=None, iD=None, cCI=None, cCSEI=None, rfI = 0):
        print name, iMD


myObj = foo(val1, val2, val3, val4, val5, val6, val7, val8, val9, val10,
    val11, val12, val13, val14, val15, val16, val17, val18, val19, val20,
    val21, val22, val23, val24, val25, val26, val27, val28)

Values of first 27 arguments will be overloaded.

I will use the function below as an example because your code does not run as posted here:

def func(name, arg1=0, arg2=0, arg3=0, arg4=0, argSkip=None, arg6=0):
    print arg1, arg2, arg3, arg4, argSkip, arg6

The values of the parameters should be passed from this list:

argument_values = [
    "value1", "value2", "value3", "value4", "value... SURPRISE! VALUE6"
]

You can get the same list, without the last parameter, by using slicing :

>>> argument_values[:-1]
['value1', 'value2', 'value3', 'value4']

You can get this sliced list and unpack it as the arguments with the * :

func('some name here', *argument_values[:-1])

Of course, you need to pas the last paramenter, that is argument_values[-1] . In this case, just use keyword arguments :

func('some name here', *argument_values[:-1], arg6=argument_values[-1])

Here is the complete script:

def func(name, arg1=0, arg2=0, arg3=0, arg4=0, argSkip=None, arg6=0):
    print arg1, arg2, arg3, arg4, argSkip, arg6

argument_values = [
    "value1", "value2", "value3", "value4", "value... SURPRISE! VALUE6"
]

func('some name here', *argument_values[:-1], arg6=argument_values[-1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM