Created
August 19, 2024 08:56
-
-
Save Attumm/d42788f6bb8ec8840aafe9ee9d47f8ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collector(until=float('inf')): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
print(until) | |
print(kwargs.pop("until", None)) | |
#until = kwargs.pop("until", None) or until | |
val = kwargs.pop("until", until) | |
for i in range(10): | |
if i >= val: | |
break | |
return func(i) | |
return wrapper | |
return decorator | |
@collector(until=5) | |
def works(item): | |
return item | |
def collector_(until=float('inf')): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
print(until) | |
print(kwargs.pop("until", None)) | |
# Added the below line, notice until on the end. | |
# for some reason that completly removes until from the function scope | |
# print(until) | |
# UnboundLocalError: cannot access local variable 'until' where it is not associated with a value | |
until = kwargs.pop("until", None) or until | |
for i in range(10): | |
if i >= until: | |
break | |
return func(i) | |
return wrapper | |
return decorator | |
@collector_(until=5) | |
def does_not(item): | |
return item | |
if __name__ == "__main__": | |
print("start") | |
print(works()) | |
print(works(until=3)) | |
print("above works") | |
print("sma") | |
print(does_not()) | |
print("stop") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start | |
5 | |
None | |
5 | |
5 | |
3 | |
5 | |
above works | |
sma | |
Traceback (most recent call last): | |
File "/Users/mbijman/Development/meesee/bug_report.py", line 52, in <module> | |
print(does_not()) | |
^^^^^^^^^^ | |
File "/Users/mbijman/Development/meesee/bug_report.py", line 26, in wrapper | |
print(until) | |
^^^^^ | |
UnboundLocalError: cannot access local variable 'until' where it is not associated with a value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment