trictionary?
Roy Smith
roy at panix.com
Sun Aug 28 22:31:47 EDT 2005
Steven Bethard <steven.bethard at gmail.com> wrote:
> In Python, pairs are usually handled with tuples[2], but tuples would be
> inconvenient in this case, since the first value must be modified.
Instead of modifying the tuple (which you can't do), you can create a new
one:
a, b = myDict[key]
myDict[key] = (a+1, b)
It's a bit inefficient, but get it working first, with clear, easy to
understand code, then worry about how efficient it is.
> Declaring a class with two attributes as
> you suggested is often a good substitute, but if the OP's code is really
> what it looks like, I get another code smell because declaring a class
> to be used by only 10 lines of code seems like overkill.
But, classes are so lightweight in Python. You can get away with nothing
more than:
class Data:
pass
and then you can do things like:
myData = Data
myData.a = a
myData.b = b
More likely, I would want to write:
class Data:
def __init__ (self, a, b):
self.a = a
self.b = b
but I've done the minimilist Data class more than once. It doesn't cost
much, and it's often more self-documenting than just a tuple.
More information about the Python-list
mailing list