开发者

Python - Noob Question - What does it mean "Giving one of the optional arguments"?

What does this do? giving one of the optional arguments:

ask_ok('OK to overwrite the file?', 2)

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
        开发者_Python百科    return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise IOError('refusenik user')
        print(complaint)

See tutorial:

http://docs.python.org/py3k/tutorial/controlflow.html#default-argument-values


In Python there are several types of arguments:

  • positional and keyword
  • named and arbitrary

The function argument in Python is an attribution operation, that is, arguments are assigned to variables in the function local namespace.

If you have a declaration like this:

def some_func(pos_arg1, pos_arg2, kw_arg1=1, kw_arg2='test'):
    print "postional arg 1 =", pos_arg1
    print "postional arg 2 =", pos_arg2
    print "keyword arg 1 =", kw_arg1
    print "keyword arg 2 =", kw_arg2

Positional arguments are mandatory and will be assigned in the given order, but keyword arguments are optional and can be called in any order - when omitted, named keyword arguments assume the declared default values (1 and 'test' in the example). So far:

>>> some_func(1) # positional arguments are mandatory
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: some_func() takes at least 2 arguments (1 given)
>>> some_func(1, 2) # this is ok
postional arg 1 = 1
postional arg 2 = 2
keyword arg 1 = 1
keyword arg 2 = test
>>> some_func(1, 2, 3) # this is also ok, keyword args may work like positional
postional arg 1 = 1
postional arg 2 = 2
keyword arg 1 = 3
keyword arg 2 = test
>>> some_func(1, 2, 3, 4) # this is also ok, keyword args may work like positional
postional arg 1 = 1
postional arg 2 = 2
keyword arg 1 = 3
keyword arg 2 = 4
>>> some_func(1, 2, kw_arg2=3) # kyword arguments may be given in any order
postional arg 1 = 1
postional arg 2 = 2
keyword arg 1 = 1
keyword arg 2 = 3

There is a problem with undeclared arguments:

>>> some_func(1, 2, 3, 4, 5)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: some_func() takes at most 4 arguments (5 given)

But you can have an arbitrary number of arguments in using the special form * and **:

>>> def some_func(pos_arg1, pos_arg2, *args, **kw_args):
...     print "postional arg 1 =", pos_arg1
...     print "postional arg 2 =", pos_arg2
...     print "other positional orgs =", args
...     print "other keyword args =", kw_args
... 
>>> some_func(1, 2, 3, 4, 5) # any number of arguments
postional arg 1 = 1
postional arg 2 = 2
other positional orgs = (3, 4, 5)
other keyword args = {}
>>> some_func(1, 2, a=3, x=4, y=5) # * and ** are optional
postional arg 1 = 1
postional arg 2 = 2
other positional orgs = ()
other keyword args = {'a': 3, 'x': 4, 'y': 5}
>>> some_func(1, 2, 'banana', 'orange', 'apple', a=3, x=4, y=5)
postional arg 1 = 1
postional arg 2 = 2
other positional orgs = ('banana', 'orange', 'apple')
other keyword args = {'a': 3, 'x': 4, 'y': 5}
>>> 

The * argument will be available as a tuple of positional arguments, and ** will be a dict of keyword arguments.

You can mix everything together but there is a rule: all keyword arguments have to be declared after positional arguments, and all the arbitrary must be after named ones.


Sets retries to 2 and leaves complaint on the default value 'Yes or no, please!'. The order of the optional arguments in the first line of the function definition is important.


This is a function.

If you are learning python I recommend you first read a good book like "Learning python". Start with simple tutorial and read a lot. After read a lot read more and more. Python is a beatifull language to start programming. Sometimes, like me, you will thing in some others languages but stay with python and you can have good results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜