sprout42/StarStruct

Easy way to access "name" of startuple

tjdevries opened this issue · 1 comments

I can't figure out if the idiomatic way of getting the name of a namedtuple. The only way I have found is to do something like:

mynamedtuple = namedtuple('this_name', ...)
name_of_tuple = mynamedtuple.__class__.__name__

That seems kind of long.

We could easily add a _name attribute to our StarTuple but I'm not sure if that would be idiomatic. Any suggestions @sprout42?

I would prefer to make it a first-class attribute, the potential problem is that the namedtuple meta class doesn't create a first-class attribute so using mynamedtuple.__class__.__name__ may break things in the future. However, I feel like it should be pretty safe...

Since this is for the startuple I feel like _name would be best (name being a fairly common field name I suspect). And if we add it in to the StarTuple class then we don't have to worry about using the __class__.__name__ attribute. So how about adding this to the StarTuple constructor:

named_tuple._name = name

Also, I'm not too concerned about being idiomatic here either.