What is the use of typename associated with a particular class?
For example,
Point = namedtuple('P', ['x', 'y'])
Where would you normally use typename ‘P’?
Thank you!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just for sanity’s sake, the first argument to namedtuple should be the same as the variable name you assign it to:
isinstance isn’t too concerned about this, although just what is ‘P’ is not known:
But pickle is one module that cares about finding the classname that matches the typename:
If I define the namedtuple as ‘Point’, then pickle is happy:
Unfortunately, it is up to you to manage this consistency. There is no way for namedtuple to know what you are assigning its output to, since assignment is a statement and not an operator in Python, so you have to pass the correct classname into namedtuple, and assign the resulting class to a variable of the same name.