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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With