Skip to content

Instantly share code, notes, and snippets.

@dusktreader
Last active March 8, 2017 21:13
Show Gist options
  • Save dusktreader/370cfe72542204570ab2151b925ece36 to your computer and use it in GitHub Desktop.
Save dusktreader/370cfe72542204570ab2151b925ece36 to your computer and use it in GitHub Desktop.
"""
I found something new today.
I find myself often missing the ++ operator in python
especially when I am wanting to assign values in tests
and would rather have an incrementing variable
"""
test_val = SomeClass(id=0)
another_val = SomeClass(id=1)
last_val = SomeClass(id=2)
"""
Obviously, if you have enough test vals, it gets annoying.
Also, the hard-coded sequence will be even more annoying if
you want to insert a line in there and want your numbering
to be sequential.
In c/c++/java/C# you would use the ++ operator to get the
current value of a counter and then increment it.
I just found the count() function from itertools in python.
Now, you can get incr equivalent functionality
"""
import itertools
i = itertools.count()
test_val = some_class(id=next(i))
another_val = some_class(id=next(i))
last_val = some_class(id=next(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment