From 8ad682164653a2653db6e0df831b71261d174367 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Sat, 1 Jan 2011 09:21:48 +0100 Subject: Changed the syntax coloring --- documentation3.html | 87 +++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 40 deletions(-) (limited to 'documentation3.html') diff --git a/documentation3.html b/documentation3.html index 2b41374..78ea3a1 100644 --- a/documentation3.html +++ b/documentation3.html @@ -369,48 +369,55 @@ function is called:

Python 3 introduced the concept of function annotations,i.e. the ability to annotate the signature of a function with additional information, stored in a dictionary named __annotations__. The decorator module, -starting from release 3.3 is able to understand and to preserve the +starting from release 3.3, is able to understand and to preserve the annotations. Here is an example:

-
-
->>> @trace
-... def f(x: 'the first argument', y: 'default argument'=1, z=2, *args:
-'varargs', **kw: 'kwargs'):
-...     pass
-
-
->>> from inspect import getfullargspec
->>> argspec = getfullargspec(f)
->>> argspec.args
-['x', 'y', 'z']
->>> argspec.varargs
-'args'
->>> argspec.varkw
-'kw'
->>> argspec.defaults
-(1, 2)
->>> argspec.kwonlyargs
-[]
->>> argspec.kwonlydefaults
->>> sorted(argspec.annotations.items())
-[('args', 'varargs'), ('kw', 'kwargs'), ('x', 'the first argument'), ('y',
-'default argument')]
-
-
+
+
>>> @trace
+... def f(x: 'the first argument', y: 'default argument'=1, z=2,
+...       *args: 'varargs', **kw: 'kwargs'):
+...     pass
+
+ +
+

In order to introspect functions with annotations, one needs the +utility inspect.getfullargspec, new in Python 3:

+
+
>>> from inspect import getfullargspec
+>>> argspec = getfullargspec(f)
+>>> argspec.args
+['x', 'y', 'z']
+>>> argspec.varargs
+'args'
+>>> argspec.varkw
+'kw'
+>>> argspec.defaults
+(1, 2)
+>>> argspec.kwonlyargs
+[]
+>>> argspec.kwonlydefaults
+>>> sorted(argspec.annotations.items())
+[('args', 'varargs'), ('kw', 'kwargs'), ('x', 'the first argument'), ('y',
+'default argument')]
+
+ +

You can also check that the __annotations__ dictionary is preserved:

-
-
->>> f.__annotations__ == f.undecorated.__annotations__
-True
-
-
-

The two dictionaries are different objects, though:

-
-
->>> id(f.__annotations__) != id(f.undecorated.__annotations__)
-True
-
-
+
+
>>> f.__annotations__ == f.undecorated.__annotations__
+True
+
+ +
+

The two dictionaries are different objects, though

+
+
>>> id(f.__annotations__) != id(f.undecorated.__annotations__)
+True
+
+ +
+

since internally the decorator module creates an entirely new dictionary +(it is not simply attaching the __annotations__ attribute to the new +function).

decorator is a decorator

-- cgit v1.2.1