Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:26:50+00:00 2026-06-03T09:26:50+00:00

What is the use of typename associated with a particular class? For example, Point

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:26:51+00:00Added an answer on June 3, 2026 at 9:26 am

    Just for sanity’s sake, the first argument to namedtuple should be the same as the variable name you assign it to:

    >>> from collections import namedtuple
    >>> Point = namedtuple('P','x y')
    >>> pp = Point(1,2)
    >>> type(pp)
    <class '__main__.P'>
    

    isinstance isn’t too concerned about this, although just what is ‘P’ is not known:

    >>> isinstance(pp,Point)
    True
    >>> isinstance(pp,P)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'P' is not defined
    

    But pickle is one module that cares about finding the classname that matches the typename:

    >>> import pickle
    >>> ppp = pickle.dumps(pp)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "c:\python26\lib\pickle.py", line 1366, in dumps
        Pickler(file, protocol).dump(obj)
      File "c:\python26\lib\pickle.py", line 224, in dump
        self.save(obj)
      File "c:\python26\lib\pickle.py", line 331, in save
        self.save_reduce(obj=obj, *rv)
      File "c:\python26\lib\pickle.py", line 401, in save_reduce
        save(args)
      File "c:\python26\lib\pickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "c:\python26\lib\pickle.py", line 562, in save_tuple
        save(element)
      File "c:\python26\lib\pickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "c:\python26\lib\pickle.py", line 748, in save_global
        (obj, module, name))
    pickle.PicklingError: Can't pickle <class '__main__.P'>: it's not found as __main__.P
    

    If I define the namedtuple as ‘Point’, then pickle is happy:

    >>> Point = namedtuple('Point','x y')
    >>> pp = Point(1,2)
    >>> ppp = pickle.dumps(pp)
    >>>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is the following use of the template-function fCompare() correct? //header template<typename _T > class
Possible Duplicate: Use 'class' or 'typename' for template parameters? I see two different template
Consider the following use of template template parameters... #include <iostream> template <typename X> class
Can I use template aliases as template template parameters? template <template <typename...> class> struct
I would like to use a template class to provide some common functionality to
Is it possible to typedef long types that use templates? For example: template <typename
I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int>
I use boost::signal with different function signatures and different combiners. In a class that
I want to use partial specialization for a template class so that all children
How can I use a static function pointer member in my class template? I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.