Skip to content

Commit 7e86a49

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Update README to clarify getclearsource behavior with multiple lambdas
and provide fallback using getsource
1 parent f889c01 commit 7e86a49

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,26 @@ print(getclearsource(lambda x: x))
102102
#> lambda x: x
103103
```
104104

105-
When working with ASTs, this is the recommended way to retrieve a function's source code.
105+
To extract only the substring containing a lambda function, the library uses AST parsing behind the scenes. Unfortunately, this does not allow it to distinguish between multiple lambda functions defined in a single line, so in this case you will get an exception:
106+
107+
```python
108+
lambdas = [lambda: None, lambda x: x]
109+
110+
getclearsource(lambdas[0])
111+
#> ...
112+
#> getsources.errors.UncertaintyWithLambdasError: Several lambda functions are defined in a single line of code, can't pick the one.
113+
```
114+
115+
If you absolutely must obtain at least some source code for these lambdas, use [`getsource`](#get-raw-source):
116+
117+
```python
118+
try:
119+
getclearsource(function)
120+
except UncertaintyWithLambdasError:
121+
getsource(function)
122+
```
123+
124+
However, in general, the `getclearsource` function is recommended for retrieving the source code of functions when working with the AST.
106125

107126

108127
## Generate source hashes

0 commit comments

Comments
 (0)