![]() |
convert a named tuple to a dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: convert a named tuple to a dictionary (/thread-36786.html) Pages:
1
2
|
RE: convert a named tuple to a dictionary - buran - Mar-31-2022 (Mar-30-2022, 07:05 PM)Skaperen Wrote: but it was not working in my 3.6.9The 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. RE: convert a named tuple to a dictionary - DeaD_EyE - Mar-31-2022 (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. RE: convert a named tuple to a dictionary - buran - Mar-31-2022 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. RE: convert a named tuple to a dictionary - Skaperen - Mar-31-2022 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. |