all() is slow?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Nov 10 18:07:23 EST 2011
On Thu, 10 Nov 2011 15:37:05 -0500, Devin Jeanpierre wrote:
>> '--' not being allowed for a name has *nothing* to do with exec, and
>> everything to do with `--` not being a valid Python identifier.
>
> The only reason valid python identifiers come into it at all is because
> they get pasted into a string where identifiers would go, and that
> string is passed to exec().
That is patently untrue. If you were implementing namedtuple without
exec, you would still (or at least you *should*) prevent the user from
passing invalid identifiers as attribute names. What's the point of
allowing attribute names you can't actually *use* as attribute names?
You could remove the validation, allowing users to pass invalid field
names, but that would be a lousy API. If you want field names that aren't
valid identifiers, the right solution is a dict, not attributes.
Here's a re-implementation using a metaclass:
http://pastebin.com/AkG1gbGC
and a diff from the Python bug tracker removing exec from namedtuple:
http://bugs.python.org/file11608/new_namedtuples.diff
You will notice both of them keep the field name validation.
--
Steven
More information about the Python-list
mailing list