mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
gh-119698: fix symtable.Class.get_methods and document its behaviour correctly (#120151)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
656a1c8108
commit
b8a8e04fec
4 changed files with 186 additions and 5 deletions
|
|
@ -180,8 +180,39 @@ Examining Symbol Tables
|
|||
|
||||
.. method:: get_methods()
|
||||
|
||||
Return a tuple containing the names of methods declared in the class.
|
||||
Return a tuple containing the names of method-like functions declared
|
||||
in the class.
|
||||
|
||||
Here, the term 'method' designates *any* function defined in the class
|
||||
body via :keyword:`def` or :keyword:`async def`.
|
||||
|
||||
Functions defined in a deeper scope (e.g., in an inner class) are not
|
||||
picked up by :meth:`get_methods`.
|
||||
|
||||
For example:
|
||||
|
||||
>>> import symtable
|
||||
>>> st = symtable.symtable('''
|
||||
... def outer(): pass
|
||||
...
|
||||
... class A:
|
||||
... def f():
|
||||
... def w(): pass
|
||||
...
|
||||
... def g(self): pass
|
||||
...
|
||||
... @classmethod
|
||||
... async def h(cls): pass
|
||||
...
|
||||
... global outer
|
||||
... def outer(self): pass
|
||||
... ''', 'test', 'exec')
|
||||
>>> class_A = st.get_children()[2]
|
||||
>>> class_A.get_methods()
|
||||
('f', 'g', 'h')
|
||||
|
||||
Although ``A().f()`` raises :exc:`TypeError` at runtime, ``A.f`` is still
|
||||
considered as a method-like function.
|
||||
|
||||
.. class:: Symbol
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue