Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two ways of passing arguments? [duplicate]

Tags:

In Python, these two examples do the same thing:

from tkinter import Label widget = Label(None, text='Hello') widget.pack() widget.mainloop()  from tkinter import Label widget = Label(None,'Hello') widget.pack() widget.mainloop() 

I think Label is a class, and when I try to create an instance of that class, I always do the same thing as in the last code example. I feel strange about the meaning of text='Hello'. Could anyone please tell me about that?

like image 470
Sayakiss Avatar asked Jun 17 '13 07:06

Sayakiss


People also ask

Which can be passed as an argument to a function?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.

How are arguments passed by value or by reference?

When you pass an argument by reference, you pass a pointer to the value in memory. The function operates on the argument. When a function changes the value of an argument passed by reference, the original value changes. When you pass an argument by value, you pass a copy of the value in memory.

What is the main advantage of passing arguments by reference?

The advantage of passing an argument ByRef is that the procedure can return a value to the calling code through that argument. The advantage of passing an argument ByVal is that it protects a variable from being changed by the procedure.

What type of parameter passing is used in Python justify your answer with sample programs?

If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It's different, if we pass mutable arguments. All parameters (arguments) in the Python language are passed by reference.


1 Answers

text='Hello' means you're explicitly passing the value 'Hello' to a keyword argument text in the function arguments.

Label(None,'Hello') means 'Hello' is passed to the second positional argument in the function definition(no matter what the name of that variable is)

>>> def func(first, second): ...     print first, second ...      >>> func('foo', 'text') foo text >>> func('foo', second = 'text') foo text 

With keyword arguments the order of calling doesn't matter, but all keyword arguments must come after positional arguments.

>>> def func(first, second, third):     print first, second, third ...      >>> func('foo', third = 'spam', second = 'bar') foo bar spam 

Here first gets the value 'foo' because of it's position, while second and third got their values because they were passed those values by explicitly using their names.

For more details read docs: http://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions

like image 184
Ashwini Chaudhary Avatar answered Oct 14 '22 07:10

Ashwini Chaudhary