Scopes and Arguments

返回

Arbitrary Arguments Examples >>> def f(*args): print args ... >>> f() () >>> f(1) (1,) >>> f(1,2,3,4) (1, 2, 3, 4) >>> >>> def f(**args): print args ... >>> f() {} >>> f(a=1,b=2) {'a': 1, 'b': 2} >>> >>> def f(a, *pargs, **kargs): print a, pargs, kargs ... >>> f(1,2,3,x=1,y=2) 1 (2, 3) {'y': 2, 'x': 1} >>> >>> def multiple(x,y): ... x = 2 ... y = [3,4] ... return x,y ... >>> X = 1 >>> L = [1,2] >>> X, L = multiple(X,L) >>> X, L (2, [3, 4]) >>> >>> def f(a,b,c): print a,b,c ... >>> f(1,2,30) 1 2 30 >>> f(c=30,b=22,a=11) 11 22 30 >>> f(111,c=333,b=222) 111 222 333 >>> >>> def f(a, b=2, c=3): print a,b,c ... >>> f(11) 11 2 3 >>> f(a = 111) 111 2 3 >>> f(1,4) 1 4 3 >>> f(1,4,5) 1 4 5 >>> f(1,c=66) 1 2 66 >>> >>> X = 99 >>> def func(Y): ... Z = X + Y ... return Z ... >>> func(1) 100 >>> import __builtin__ >>> dir(__builtin__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning', 'E OFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointEr ror', 'FutureWarning', 'IOError', 'ImportError', 'IndentationError', 'IndexError ', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', ' None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'Ove rflowWarning', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'R untimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning' , 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalErr or', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTransla teError', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionE rror', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'apply', 'b asestring', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod ', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'getattr' , 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', ' isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', ' map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', ' range', 'raw_input', 'reduce', 'reload', 'repr', 'round', 'setattr', 'slice', 's taticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars ', 'xrange', 'zip'] >>> zip <built-in function zip> >>> X = 88 >>> def func(): ... X = 99 ... >>> func() >>> print X 88 >>> >>> X = 88 >>> X 88 >>> def func(): ... global X ... X = 9 ... >>> func() >>> print X 9 >>> >>> y, z = 1, 2 >>> >>> def all_global(): ... global x ... x = y + z ... >>> x Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'x' is not defined >>> print x Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'x' is not defined >>> all_global() >>> print x 3 >>> >>> def f1(): ... x = 88 ... def f2(): ... print x ... f2() ... >>> f1() 88 >>> def f1(): ... x = 88 ... def f2(): ... print x ... return f2() ... >>> action = f1() 88 >>> action() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: 'NoneType' object is not callable >>> def f1(): ... x = 88 ... def f2(x=x): ... print x ... f2() ... >>> f1() 88 >>> >>> def func(): ... x = 4 ... action = (lambda n: x ** n) ... return action ... >>> x = func() >>> print x(2) 16 >>> def f1(): ... x = 99 ... def f2(): ... def f3(): ... print x ... f3() ... f2() ... >>> f1() 99 >>> Arguments and Shared References >>> def changer(x,y): ... x = 2 ... y[0] = 'spam' ... >>> X = 1 >>> L = [1,2] >>> changer(X,L) >>> X,L (1, ['spam', 2]) >>> >>> L = [1,2] >>> changer(X,L[:]) # Pass a copy, so my L doest not change >>> def changer(x,y): ... y = y[:] ... x = 2 ... y[0] = 'spam' ... >>> L = [1,2] >>> changer(X, tuple(L)) Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 4, in changer TypeError: object doesn't support item assignment # Pass a tuple, so changes are errors
返回