-
Hi, can we have the feature to return tqdm instance in a loop? class tqdm():
def __init__(self, ..., return_bar=False):
pass
def __iter__(self):
....
for obj in iterable:
if self.return_bar:
yield obj, self
else:
yield obj So we can use fewer Indentations and write less code for i, pbar in tqdm(range(3), return_bar=True):
time.sleep(.5)
pbar.set_postfix_str(f'i ** 2: {i ** 2}') and the original version with tqdm(total=3) as pbar:
for i in range(3):
time.sleep(.5)
pbar.set_postfix_str(f'i ** 2: {i ** 2}')
pbar.update() I can make a pr if it's okay |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I can see the appeal of less indentation but the suggestion is very bad in terms of performance/speed. If you don't mind swapping indentation for 2 extra lines, try this: pbar = tqdm(range(3))
for i in pbar:
time.sleep(.5)
pbar.set_postfix_str(f'i ** 2: {i ** 2}')
pbar.close() |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick response. I haven't considered the performance issue. The suggested solution looks fine! |
Beta Was this translation helpful? Give feedback.
I can see the appeal of less indentation but the suggestion is very bad in terms of performance/speed.
If you don't mind swapping indentation for 2 extra lines, try this: