Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
named tuples
#3
(May-02-2017, 04:55 AM)Larz60+ Wrote: type(tuple_name).__name__

this just gives me the original name of the object, not the list of keys.

type(sys.version_info).__name__ -> 'version_info' but i am wanting whatever(sys.version_info) -> ('major', 'minor', 'micro', 'releaselevel', 'serial') or something like that (a list, a set, a dict, or whatever).  please note that sys.version_info is just one possible example.  a different (any) named tuple may be involved, so don't try to focus on that one case.



you know how

d = some dictionary
[x for x in d]
gives a list of keys in the dictionary.  doing that with a named tuple gives a list of the values.  there is no .keys() for a named tuple and and dir() of the name tuple includes other attributes.  and even if you can remove the other attributes from dir(), the order is sorted, not the original order (tuples have an order).

i just know someone is going to say "dictionaries and tuples are different".  i know that.  that's why i am asking.

well... much of the real reason i am asking is to figure out how to code for them in a revision of my print_object() function in my printobject module.  i want it to produce an expression that can be embedded in python source code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
named tuples - by Skaperen - May-02-2017, 03:34 AM
RE: named tuples - by Larz60+ - May-02-2017, 04:55 AM
RE: named tuples - by Skaperen - May-02-2017, 06:32 AM
RE: named tuples - by volcano63 - May-02-2017, 08:17 AM
RE: named tuples - by Skaperen - May-02-2017, 09:29 AM
RE: named tuples - by Mekire - May-02-2017, 09:42 AM
RE: named tuples - by volcano63 - May-02-2017, 11:50 AM
RE: named tuples - by Skaperen - May-03-2017, 07:24 AM
RE: named tuples - by Mekire - May-03-2017, 07:29 AM
RE: named tuples - by Skaperen - May-03-2017, 07:34 AM
RE: named tuples - by Mekire - May-03-2017, 08:02 AM
RE: named tuples - by Skaperen - May-03-2017, 08:20 AM
RE: named tuples - by Mekire - May-03-2017, 08:27 AM
RE: named tuples - by Skaperen - May-03-2017, 08:54 AM
RE: named tuples - by Skaperen - May-05-2017, 02:45 AM
RE: named tuples - by Larz60+ - May-05-2017, 05:34 AM
RE: named tuples - by Skaperen - May-05-2017, 09:20 AM
RE: named tuples - by Larz60+ - May-05-2017, 05:06 PM
RE: named tuples - by Skaperen - May-06-2017, 08:23 AM
RE: named tuples - by Larz60+ - May-06-2017, 02:00 PM
RE: named tuples - by Skaperen - May-07-2017, 04:37 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020