23

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?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Sayakiss
  • 6,878
  • 8
  • 61
  • 107

4 Answers4

43

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

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
14

I feel strange about the meaning of text='Hello'. Could anyone please tell me about that?

It is a named argument or keyword argument.

Named arguments allow to pass arguments to functions in any order by not only passing the argument value, but also the argument name.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
7

As others said, this is a keyword argument. Pragmatically, the difference between specifying or not the argument is simply which argument is going to be used.

For example, if you had a function with 3 arguments, and only passed one, or two of them, you'd have to specify which argument you were using, in case they were not the first ones in your definition.

def f(a = "a", b = "b", c = "c"):
    print a, b, c

>>> f("x")
x b c
>>> f("x", "y")
x y c
>>> f("x", c = "y")
x b y
Rubens
  • 14,478
  • 11
  • 63
  • 92
6

text in this case is a keyword argument, which means that it is optional. If you don't supply it explicitly, a default value will be supplied automatically.

bbayles
  • 4,389
  • 1
  • 26
  • 34
  • You can pass non-optional arguments by keyword. Ashwini Chaudhary's example code does this, for instance. I think you're confusing them, because the syntax for a call with keyword arguments looks similar to a definition with default parameters: `foo(bar=baz)`. – Blckknght Jun 17 '13 at 12:02
  • I'm aware of the nuance, but left it out of the answer since the linked docs explain things pretty well. Note also that with some functions it's not a good idea to use keyword arguments even when it's possible to do so, see [randrange](http://docs.python.org/3.3/library/random.html#random.randrange). – bbayles Jun 17 '13 at 17:58