Python Forum
convert a named tuple to a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert a named tuple to a dictionary
#11
(Mar-30-2022, 07:05 PM)Skaperen Wrote: but it was not working in my 3.6.9
The method is available long before that, even in python2. Here is the docs for python 3.6
If it is "not working" then there is problem in how you try to use it - show minimal reproducible example of your code that does not work, as well as full traceback you get (if any).

BTW, my question "why" was targeted at others, suggesting functions that mimic the method.
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
(Mar-31-2022, 05:38 AM)buran Wrote: BTW, my question "why" was targeted at others, suggesting functions that mimic the method.

It helps to understand how Python works. Looking in existing Python-Code, replicating this with own functions, may help to understand things better.

The other thing is that objects/functions/methods with a leading underscore should not be used because they are "private" for this module. Everyone was told NOT to use _name. Then the people think,
Quote:OH, I shouldn't use it, let's make an own function.

For this reason, you often find replicated code that exists in the modules, but unfortunately starts with an underscore.

On the other side, the people aren't concerned about sys._getframe.
import sys

def i_was_called_from():
    print("I was called from:", sys._getframe().f_back.f_code.co_name)


def foo():
    i_was_called_from()


foo()
Useful function and it's impossible to remove it because it's used in many projects.
It's a bit awkward. I want to have sys.getframe() (no underscore) and an asdict function without underscore is also nice.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#13
By the way, this SO answer is interesting and adds to the whole discussion as well as provide reasoning for the leading underscore
https://stackoverflow.com/a/26180604/4046632

Quote:This is a documented method of namedtuples, i.e. unlike the usual convention in python the leading underscore on the method name isn't there to discourage use. Along with the other methods added to namedtuples, _make, _replace, _source, _fields, it has the underscore only to try and prevent conflicts with possible field names.
DeaD_EyE likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
the PDF docs for 3.6 were goofed up so i d/l the 3.7 version (months ago). i found "_asdict" in there but failed to check for any version notes. now i am looking for that code that tested it. i will check backups, tonight, to see if those captured it.
i suspect people distrust the leading underscore because a module in Python excludes them from being imported. but this is an attribute.
Tradition is peer pressure from dead people

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to convert tuple value into string mg24 2 2,357 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  convert List with dictionaries to a single dictionary iamaghost 3 2,877 Jan-22-2021, 03:56 PM
Last Post: iamaghost
Sad Convert python list to dictionary akanowhere 6 3,469 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,841 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Updating dictionary with tuple Mark17 2 2,016 Aug-06-2020, 02:59 PM
Last Post: Mark17
  how to get the keys in a named tuple Skaperen 5 10,864 Mar-25-2020, 09:51 PM
Last Post: Skaperen
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,212 May-21-2019, 11:39 AM
Last Post: avorane
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 51,814 Apr-27-2019, 03:13 AM
Last Post: perfringo
  Converting List of 3 Element Tuple to Dictionary fooikonomou 11 5,847 Jan-14-2019, 09:51 AM
Last Post: perfringo
  unable to convert text file in dictionary purnima1 6 4,341 Apr-02-2018, 07:44 AM
Last Post: purnima1

Forum Jump:

User Panel Messages

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