WildBill
WildBill

Reputation: 9291

Default parameter in python with objects

I know I can define default parameters in python, but can I do so with objects?

For example, I'd like to work with a p.expect object:

def exitDevice(ip, m='', sendExit=True):
    if sendExit:
        m.send('exit')

    print "left device", ip

Is that the correct way to handle an object passed in as a default argument in Python? If not, how does one do so? Or if this is correct is there a better way to do so?

Upvotes: 0

Views: 138

Answers (1)

wroniasty
wroniasty

Reputation: 8052

It's a bit tricky, since the default value has to be defined at the time your code is parsed, but you can always do something like this:

def exitDevice(ip,m=None,sendExit=True):
   if m is None: m = getDefaultValueForM()
   if sendExit: m.send ( 'exit' )

Upvotes: 4

Related Questions