Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between namedtuple and a frozen dict

I know there is no frozen dict data type but If there was one how would it be different from namedtuple. It seems to me that namedtuple performs the required operations but obviously it does not. So what are the differences?

like image 831
jamylak Avatar asked Mar 31 '12 23:03

jamylak


1 Answers

From an API viewpoint, probably not much, though:

  • namedtuple keys are always strings with some limitations:

Any valid Python identifier may be used for a fieldname except for names starting with an underscore. Valid identifiers consist of letters, digits, and underscores but do not start with a digit or underscore and cannot be a keyword such as class, for, return, global, pass, or raise.

  • namedtuples can always be accessed as regular tuples.

Internally they are very different:

Named tuple instances do not have per-instance dictionaries, so they are lightweight and require no more memory than regular tuples.

like image 86
Karoly Horvath Avatar answered Nov 10 '22 23:11

Karoly Horvath