Skip to content

Commit 4bea978

Browse files
committed
Add examples for inspect.
1 parent a9013f9 commit 4bea978

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

Doc/whatsnew/3.2.rst

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ functools
742742

743743
(Contributed by Raymond Hettinger.)
744744

745-
* To aid in porting programs from Python 2, the :func:`~functools.cmp_to_key`
745+
* To aid in porting programs from Python 2, the :func:`functools.cmp_to_key`
746746
function converts an old-style comparison function to
747747
modern :term:`key function`:
748748

@@ -758,7 +758,7 @@ itertools
758758
---------
759759

760760
* The :mod:`itertools` module has a new :func:`~itertools.accumulate` function
761-
modeled on APL's *scan* operator and on Numpy's *accumulate* function:
761+
modeled on APL's *scan* operator and Numpy's *accumulate* function:
762762

763763
>>> list(accumulate(8, 2, 50))
764764
[8, 10, 60]
@@ -1372,14 +1372,41 @@ inspect
13721372

13731373
* The :mod:`inspect` module has a new function
13741374
:func:`~inspect.getgeneratorstate` to easily identify the current state of a
1375-
generator as one of ``GEN_CREATED``, ``GEN_RUNNING``, ``GEN_SUSPENDED`` or
1376-
``GEN_CLOSED``. (Contributed by Rodolpho Eckhardt and Nick Coghlan,
1377-
:issue:`10220`.)
1375+
generator as one of *GEN_CREATED*, *GEN_RUNNING*, *GEN_SUSPENDED* or
1376+
*GEN_CLOSED*::
1377+
1378+
>>> def gen():
1379+
yield 'one'
1380+
yield 'two'
1381+
>>> g = gen()
1382+
>>> inspect.getgeneratorstate(g)
1383+
'GEN_CREATED'
1384+
>>> next(g)
1385+
'one'
1386+
>>> inspect.getgeneratorstate(g)
1387+
'GEN_SUSPENDED'
1388+
1389+
(Contributed by Rodolpho Eckhardt and Nick Coghlan, :issue:`10220`.)
13781390

13791391
* To support lookups without the possibility of activating a dynamic attribute,
13801392
the :mod:`inspect` module has a new function, :func:`~inspect.getattr_static`.
13811393
Unlike :func:`hasattr`, this is a true read-only search, guaranteed not to
1382-
change state while it is searching. (Contributed by Michael Foord.)
1394+
change state while it is searching::
1395+
1396+
>>> class A:
1397+
@property
1398+
def f(self):
1399+
print('Running')
1400+
return 10
1401+
1402+
>>> a = A()
1403+
>>> getattr(a, 'f')
1404+
Running
1405+
10
1406+
>>> inspect.getattr_static(a, 'f')
1407+
<property object at 0x1022bd788>
1408+
1409+
(Contributed by Michael Foord.)
13831410

13841411
pydoc
13851412
-----

0 commit comments

Comments
 (0)