0

When working with a namedtuple value returned by an imported module, ._asdict() is not found to be a valid attribute.

My attempted usage :

>>> import os
>>> os.get_terminal_size()
os.terminal_size(columns=120, lines=30)
>>> os.get_terminal_size()._asdict()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'os.terminal_size' object has no attribute '_asdict'

Reading over the source for the os module I noted the following :

terminal_size = namedtuple("terminal_size", "columns lines")

# ...

def get_terminal_size(fallback=(80, 24)):

    # ...

    return terminal_size(columns, lines)

As the returned value is a namedtuple I would expect it to retain the functionalities this data type provides. I would expect the following :

>>> import os
>>> os.get_terminal_size()
os.terminal_size(columns=120, lines=30)
>>> os.get_terminal_size()._asdict()
{'columns': 120, 'lines': 30}

Despite the leading underscore, this functionality is documented and is commonly utilized. Link to this documentation and the source referenced can be found below.

I am new to namedtuples and I appreciate any guidance. Your time is much appreciated.

https://docs.python.org/3/library/collections.html#collections.somenamedtuple._asdict https://github.com/Snaipe/python-rst2ansi/blob/master/rst2ansi/get_terminal_size.py

0 Answers0