@@ -3529,4 +3529,194 @@ \section{Miscellaneous}
35293529\end {itemize }
35303530
35313531
3532+ \chapter {New in Release 1.3 }
3533+
3534+
3535+ This chapter describes yet more recent additions to the Python
3536+ language and library.
3537+
3538+
3539+ \section {New Keyword Arguments }
3540+
3541+ Functions and methods written in Python can now be called using
3542+ keyword arguments of the form \code {\var {keyword} = \var {value}}. For
3543+ instance, the following function:
3544+
3545+ \begin {verbatim }
3546+ def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
3547+ print "-- This parrot wouldn't", action,
3548+ print "if you put", voltage, "Volts through it."
3549+ print "-- Lovely plumage, the", type
3550+ print "-- It's", state, "!"
3551+ \end {verbatim }
3552+
3553+ could be called in any of the following ways:
3554+
3555+ \begin {verbatim }
3556+ parrot(1000)
3557+ parrot(action = 'VOOOOOM', voltage = 1000000)
3558+ parrot('a thousand', state = 'pushing up the daisies')
3559+ parrot('a million', 'bereft of life', 'jump')
3560+ \end {verbatim }
3561+
3562+ but the following calls would all be invalid:
3563+
3564+ \begin {verbatim }
3565+ parrot() # required argument missing
3566+ parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
3567+ parrot(110, voltage=220) # duplicate value for argument
3568+ parrot(actor='John Cleese') # unknown keyword
3569+ \end {verbatim }
3570+
3571+ In general, an argument list must have the form: zero or more
3572+ positional arguments followed by zero or more keyword arguments, where
3573+ the keywords must be chosen from the formal parameter names. It's not
3574+ important whether a formal parameter has a default value or not. No
3575+ argument must receive a value more than once -- formal parameter names
3576+ corresponding to positional arguments cannot be used as keywords in
3577+ the same calls.
3578+
3579+ Note that no special syntax is required to allow a function to be
3580+ called with keyword arguments. The additional costs incurred by
3581+ keyword arguments are only present when a call uses them.
3582+
3583+ (As far as I know, these rules are exactly the same as used by
3584+ Modula-3, even if they are enforced by totally different means. This
3585+ is intentional.)
3586+
3587+ When a final formal parameter of the form \code {**\var {name}} is
3588+ present, it receives a dictionary containing all keyword arguments
3589+ whose keyword doesn't correspond to a formal parameter. This may be
3590+ combined with a formal parameter of the form \code {*\var {name}} which
3591+ receives a tuple containing the positional arguments beyond the formal
3592+ parameter list. (\code {*\var {name}} must occur before
3593+ \code {**\var {name}}.) For example, if we define a function like this:
3594+
3595+ \begin {verbatim }
3596+ def cheeseshop(kind, *arguments, **keywords):
3597+ print "-- Do you have any", kind, '?'
3598+ print "-- I'm sorry, we're all out of", kind
3599+ for arg in arguments: print arg
3600+ print '-'*40
3601+ for kw in keywords.keys(): print kw, ':', keywords[kw]
3602+ \end {verbatim }
3603+
3604+ It could be called like this:
3605+
3606+ \begin {verbatim }
3607+ cheeseshop('Limburger', "It's very runny, sir.",
3608+ "It's really very, VERY runny, sir.",
3609+ client='John Cleese',
3610+ shopkeeper='Michael Palin',
3611+ sketch='Cheese Shop Sketch')
3612+ \end {verbatim }
3613+
3614+ and of course it would print:
3615+
3616+ \begin {verbatim }
3617+ -- Do you have any Limburger ?
3618+ -- I'm sorry, we're all out of Limburger
3619+ It's very runny, sir.
3620+ It's really very, VERY runny, sir.
3621+ ----------------------------------------
3622+ client : John Cleese
3623+ shopkeeper : Michael Palin
3624+ sketch : Cheese Shop Sketch
3625+ \end {verbatim }
3626+
3627+ Side effects of this change include:
3628+
3629+ \begin {itemize }
3630+
3631+ \item
3632+ In the effort of implementing keyword arguments, function and
3633+ especially method calls have been sped up significantly -- for a
3634+ method with ten formal parameters, the call overhead has been cut in
3635+ half; for a function with one formal parameters, the overhead has been
3636+ reduced by a third.
3637+
3638+ \item
3639+ The format of \code {.pyc} files has changed (again).
3640+
3641+ \end {itemize }
3642+
3643+ \section {Minor Changes }
3644+
3645+ \begin {itemize }
3646+
3647+ \item
3648+ For file objects, \code {\var {f}.read(0)} and
3649+ \code {\var {f}.readline(0)} now return an empty string rather than
3650+ reading an unlimited number of bytes. For the latter, omit the
3651+ argument altogether or pass a negative value.
3652+
3653+ \item
3654+ A new system variable, \code {sys.platform}, has been added. It
3655+ specifies the current platform, e.g. \code {sunos5} or \code {linux1}.
3656+
3657+ \item
3658+ The built-in functions \code {input()} and \code {raw_input()} now use
3659+ the GNU readline library when it has been configured (formerly, only
3660+ interactive input to the interpreter itself was read using GNU
3661+ readline). The GNU readline library provides elaborate line editing
3662+ and history. The Python debugger (\code {pdb}) is the first
3663+ beneficiary of this change.
3664+
3665+ \item
3666+ Two new built-in functions, \code {globals()} and \code {locals()},
3667+ provide access to dictionaries containming current global and local
3668+ variables, respectively. (These augment rather than replace
3669+ \code {vars()}, which returns the current local variables when called
3670+ without an argument, and a module's global variables when called with
3671+ an argument of type module.)
3672+
3673+ \item
3674+ The optional built-in modules \code {dbm} and \code {gdbm} are more
3675+ coordinated --- their \code {open()} functions now take the same values
3676+ for their \var {flag} argument, and the \var {flag} and \var {mode}
3677+ argument have default values (to open the database for reading only,
3678+ and to create the database with mode \code {0666} minuse the umask,
3679+ respectively).
3680+
3681+ \item
3682+ A new dbm-like module, \code {dbhash}, has been added, which uses the
3683+ BSD DB package's hash method.
3684+
3685+ \item
3686+ The \code {raise} statement now takes an optional argument which
3687+ specifies the traceback to be used when printing the exception's stack
3688+ trace. This must be a traceback object, such as found in
3689+ \code {sys.exc_traceback}. When omitted or given as \code {None}, the
3690+ old behavior (to generate a stack trace entry for the current stack
3691+ frame) is used.
3692+
3693+ \item
3694+ The built-in function \code {compile()} now takes a third possible
3695+ value for the kind of code to be compiled: specifying \code {'single'}
3696+ generates code for a single interactive statement, which prints the
3697+ output of expression statements that evaluate to something else than
3698+ \code {None}.
3699+
3700+ \item
3701+ The tokenizer is now more tolerant of alien whitespace. Control-L in
3702+ the leading whitespace of a line resets the column number to zero,
3703+ while Control-R just before the end of the line is ignored.
3704+
3705+ \item
3706+ The dynamic module loader recognizes the fact that different filenames
3707+ point to the same shared library and loads the library only once, so
3708+ you can have a single shared library that defines multiple modules.
3709+ (SunOS / SVR4 style shared libraries only.)
3710+
3711+ \item
3712+ Jim Fulton's `` abstract object interface'' has been incorporated into
3713+ the run-time API. For more detailes, read the files
3714+ \code {Include/abstract.h} and \code {Objects/abstract.c}.
3715+
3716+ \item
3717+ Numerous things I have forgotten or that are so obscure no-one will
3718+ notice them anyway :-)
3719+
3720+ \end {itemize }
3721+
35323722\end {document }
0 commit comments