Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should namedtuples follow constant name conventions in python?

I've written a small python module where I use a couple of namedtuples to pass info around because I find them very expressive. I considered these types and named them following the PEP8 convention for class names (CamelCased). However pylint sees the line:

PersonData = collections.namedtuple('PersonData', 'name surname age')

at the module's global scope and goes: Invalid constant name 'PersonData'.

Am I misusing namedtuples? What's the pythonic recommendation? I can only think of suppressing the warning, renaming the structure to PERSON_DATA, or making it a full class. Note that, in my case, it wouldn't make sense for it to have methods though.

If the answer is to suppress the warning. Wouldn't this be a recurring problem with pylint vs named tuples?

(using pylint-0.26.0, python-2.7.4)

like image 230
Luis Avatar asked Nov 14 '13 08:11

Luis


People also ask

What is an advantage of Namedtuples over dictionaries?

Moreover, as namedtuple instances do not have per-instance dictionaries, they are lightweight and require no more memory than regular tuples. This makes them faster than dictionaries.

How do you name a tuple in Python?

To create a named tuple, import the namedtuple class from the collections module. The constructor takes the name of the named tuple (which is what type() will report), and a string containing the fields names, separated by whitespace. It returns a new namedtuple class for the specified fields.

Can named tuples have methods?

index() , namedtuple classes also provide three additional methods and two attributes. To prevent name conflicts with custom fields, the names of these attributes and methods start with an underscore. In this section, you'll learn about these methods and attributes and how they work.

Can named tuples be modified?

Since a named tuple is a tuple, and tuples are immutable, it is impossible to change the value of a field.


3 Answers

This issue has been solved in newer versions of pylint.

My system was picking the version from the ubuntu (13.04) repositories (pylint-0.26.0). Using a virtualenv I could pip install pylint-1.0.0 which recognizes named tuples and will actually enforce using the same format as classes.

Note that it won't pick it up if you're doing string manipulation on the fields string. For example:

PersonData = collections.namedtuple('PersonData', 'name ' + 'surname age')

will still cause pylint-1.0.0 to spit out the error code for invalid constant name. In this case the only workaround is to disable the warning as per Martijn Pieters suggestion.

like image 165
Luis Avatar answered Nov 15 '22 21:11

Luis


You can ignore pylint here, you are using the camel case naming convention exactly right.

You can suppress the warning:

PersonData = collections.namedtuple('PersonData', 'name surname age')  # pylint: disable-msg=C0103

namedtuple is a class factory, so use the naming conventions for a class.

like image 34
Martijn Pieters Avatar answered Nov 15 '22 19:11

Martijn Pieters


This has been fixed in pylint-1.0.0 (see Pylint's Changelog):

For toplevel name assignment, the class name regex will be used if pylint can detect that value on the right-hand side is a class (like collections.namedtuple()).

Note that the namedtuple must be defined on the toplevel, if defined e.g. within a function pylint will still give a invalid-name warning.

like image 40
dsandbrink Avatar answered Nov 15 '22 20:11

dsandbrink