[Python-ideas] namedtuple literals [Was: RE a new namedtuple]
toddrjen at gmail.com
Sun Jul 23 12:08:03 EDT 2017
On Jul 20, 2017 1:13 AM, "David Mertz" <mertz at gnosis.cx> wrote:
I'm concerned in the proposal about losing access to type information (i.e.
name) in this proposal. For example, I might write some code like this now:
>>> from collections import namedtuple
>>> Car = namedtuple("Car", "cost hp weight")
>>> Motorcycle = namedtuple("Motorcycle", "cost hp weight")
>>> smart = Car(18_900, 89, 949)
>>> harley = Motorcyle(18_900, 89, 949)
>>> if smart==harley and type(smart)==type(harley):
... print("These are identical vehicles")
The proposal to define this as:
>>> smart = (cost=18_900, hp=89, weight=949)
>>> harley = (cost=18_900, hp=89, weight=949)
Doesn't seem to leave any way to distinguish the objects of different types
that happen to have the same fields. Comparing `
smart._fields==harley._fields` doesn't help here, nor does any type
constructed solely from the fields.
What about making a syntax to declare a type? The ones that come to mind are
name = (x=, y=)
name = (x=pass, y=pass)
They may not be clear enough, though.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas