问题
Assuming this code snippet (ipython)
In [1]: from collections import namedtuple
In [2]: type(namedtuple)
Out[2]: function
you can see that factory function namedtuple
is a function
, of course. I can't see any hint in PEP-8 to use the naming convention for classes in that case. I can only see that the documentation treating it like a class as a naming convention.
Why should one use the naming convention for classes in that case?
回答1:
You're referring to the use of the name Point
for the type returned by namedtuple
. namedtuple
is a function alright, but it returns a class (a thing that has the type type
). And by PEP-8, class names should look LikeThis.
In [1]: from collections import namedtuple
In [2]: Point = namedtuple('Point', ['x', 'y'])
In [3]: type(Point)
Out[3]: type
来源:https://stackoverflow.com/questions/33259220/why-should-i-use-camelcase-for-namedtuple