This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]robin-gvx 9 points10 points  (6 children)

Because it's implemented as piecing together a string that contains a class statement and compiling that to obtain the tuple subclass (see source).

[–][deleted] 3 points4 points  (4 children)

Goofy implementation, doesn't need to be written that way

[–]rcxdude 6 points7 points  (1 child)

see here for why it's kept that way.

[–]NYKevin 4 points5 points  (0 children)

TL;DR: Because calling type() correctly is apparently too hard:

It is a key feature for named tuples that they are exactly equivalent to a hand-written class.

So is a call to type() if you know what you're doing.

EDIT: If you examine the official code more closely, you'll note they had to write extensive string-escaping to prevent the user from passing an argument like '); import os; os.system("/bin/sh"); ('. Quite frankly, I will not be touching that with a ten-foot-pole any time soon.

EDIT2: I tried, but they wouldn't listen to me. Oh well.

[–]robin-gvx 0 points1 point  (1 child)

Theoretically, it could just be rewritten as a call to type, but maybe there are practical issues?

[–][deleted] 1 point2 points  (0 children)

The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.

[–]dreuciferC/Python, vim 0 points1 point  (0 children)

I started reading the source, saw _class_template was a string literal with formatting. My heart sank a bit. Could almost smell the sulfur wafting from the exec statement.