some other minor updates
This commit is contained in:
parent
8945c589a4
commit
f22bcae049
35
pep-0318.txt
35
pep-0318.txt
|
@ -165,9 +165,9 @@ This is equivalent to::
|
||||||
pass
|
pass
|
||||||
func = dec2(dec1(func))
|
func = dec2(dec1(func))
|
||||||
|
|
||||||
The decorators are near the declaration of the function's API but are
|
without the intermediate assignment to the variable ``func``. The
|
||||||
clearly secondary. The @ sign makes it clear that something new is
|
decorators are near the function declaration. The @ sign makes it
|
||||||
going on here.
|
clear that something new is going on here.
|
||||||
|
|
||||||
The decorator statement is limited in what it can accept - arbitrary
|
The decorator statement is limited in what it can accept - arbitrary
|
||||||
expressions will not work. Guido preferred this because of a `gut feeling`_
|
expressions will not work. Guido preferred this because of a `gut feeling`_
|
||||||
|
@ -270,6 +270,14 @@ these, stating::
|
||||||
their design needs to be forward-looking, not constrained by what
|
their design needs to be forward-looking, not constrained by what
|
||||||
can be implemented in 2.3.
|
can be implemented in 2.3.
|
||||||
|
|
||||||
|
A `page on the Python Wiki`_ was created to summarize a number of the
|
||||||
|
proposals. Once it stabilizes perhaps someone would care to
|
||||||
|
incorporate its content into this PEP (hint, hint).
|
||||||
|
|
||||||
|
.. page on the Python Wiki:
|
||||||
|
http://www.python.org/moin/PythonDecorators
|
||||||
|
|
||||||
|
|
||||||
Why @?
|
Why @?
|
||||||
------
|
------
|
||||||
|
|
||||||
|
@ -340,7 +348,8 @@ some examples of use.
|
||||||
atexit.register(f)
|
atexit.register(f)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
def func() [onexit]:
|
@onexit
|
||||||
|
def func():
|
||||||
...
|
...
|
||||||
|
|
||||||
2. Define a class with a singleton instance. Note that once the class
|
2. Define a class with a singleton instance. Note that once the class
|
||||||
|
@ -357,7 +366,8 @@ some examples of use.
|
||||||
return instances[cls]
|
return instances[cls]
|
||||||
return getinstance
|
return getinstance
|
||||||
|
|
||||||
class MyClass [singleton]:
|
@singleton
|
||||||
|
class MyClass:
|
||||||
...
|
...
|
||||||
|
|
||||||
3. Add attributes to a function. (Based on an example posted by
|
3. Add attributes to a function. (Based on an example posted by
|
||||||
|
@ -372,8 +382,9 @@ some examples of use.
|
||||||
return f
|
return f
|
||||||
return decorate
|
return decorate
|
||||||
|
|
||||||
def mymethod(f) [attrs(versionadded="2.2",
|
@attrs(versionadded="2.2",
|
||||||
author="Guido van Rossum")]:
|
author="Guido van Rossum")
|
||||||
|
def mymethod(f):
|
||||||
...
|
...
|
||||||
|
|
||||||
4. Enforce function argument and return types. (Note that this is not
|
4. Enforce function argument and return types. (Note that this is not
|
||||||
|
@ -403,8 +414,9 @@ some examples of use.
|
||||||
return new_f
|
return new_f
|
||||||
return check_returns
|
return check_returns
|
||||||
|
|
||||||
def func(arg1, arg2) [accepts(int, (int,float)),
|
@accepts(int, (int,float))
|
||||||
returns((int,float))]:
|
@returns((int,float))
|
||||||
|
def func(arg1, arg2):
|
||||||
return arg1 * arg2
|
return arg1 * arg2
|
||||||
|
|
||||||
5. Declare that a class implements a particular (set of) interface(s).
|
5. Declare that a class implements a particular (set of) interface(s).
|
||||||
|
@ -429,10 +441,11 @@ some examples of use.
|
||||||
class IBar(Interface):
|
class IBar(Interface):
|
||||||
"""Declare something about IBar here"""
|
"""Declare something about IBar here"""
|
||||||
|
|
||||||
class Foo(object) [provides(IBar)]:
|
@provides(IBar)
|
||||||
|
class Foo(object):
|
||||||
"""Implement something here..."""
|
"""Implement something here..."""
|
||||||
|
|
||||||
Of course, all these examples are possible today, though without the
|
Of course, all these examples are possible today, though without
|
||||||
syntactic support.
|
syntactic support.
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue