0

I'm getting unexpected behavior in Python 2.7 when using default arguments. Instead of using the value passed in by the caller, one argument is getting overridden by the default. Consider the following functions:

def vm_list(domain, username, server, verbose, cluster, datacenter, name,
            regex, template_only, detailed_list, vm_status):

    print 'inside vm_list vm_stauts is', vm_status
    vcenter_request = "get_registered_vms"
    vmlist = vcenter_connect(server, user, password, vcenter_request, vm_status)

def vcenter_connect(server, user, password, request,
                    source_vm=None, target_vm=None,
                    res_pool=None, num=1, vm_status=None):

    print 'inside vcenter_connect vm_stauts is', vm_status

vm_list gets called first with vm_status passed in as a string, such as "poweredOff". Inside this function vm_status is equal to "poweredOff" as expected, but when I pass the value to vcenter_connect it's getting overridden by the default argument None. I am using other default arguments in vcenter_connect but they are working as expected, that is, they're only using the default values when nothing is passed in.

Running the relevent parts to test the string value gives me this:

inside vm_list vm_stauts is poweredOff
inside vcenter_connect vm_stauts is None

What am I doing wrong?

1 Answer 1

2

You pass vm_status as the 5th positional parameter to vcenter_connect, but the 5th positional parameter to that function is source_vm, so vm_status gets its default of None.

Instead, you must tell it which keyword parameter you are sending after the first 4 positional ones:

vmlist = vcenter_connect(server, user, password, vcenter_request, vm_status=vm_status)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That does the trick. This is a learning step for me as I'm not quite used to keyword parameters.

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.