Skip to content

Instantly share code, notes, and snippets.

@Attumm
Created August 19, 2024 08:56
Show Gist options
  • Save Attumm/d42788f6bb8ec8840aafe9ee9d47f8ae to your computer and use it in GitHub Desktop.
Save Attumm/d42788f6bb8ec8840aafe9ee9d47f8ae to your computer and use it in GitHub Desktop.
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")
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