I'm writing a set of python functions that perform some sort of conformance checking on a source code project. I'd like to specify quite verbose names for these functions, e.g.: check_5_theVersionOfAllVPropsMatchesTheVersionOfTheAutolinkHeader()
Could such excessively long names be a problem for python? Is there a maximum length for attribute names?
-
6Please don't do this. Long names aren't always bad, but they are a code smell. In this case I would shorten the name and put a more detailed description in a doc string. Also, I would either log the results verbosely or throw a clearly written exception explaining the failed check.Steven Rumbalski– Steven Rumbalski2013年06月04日 15:20:08 +00:00Commented Jun 4, 2013 at 15:20
5 Answers 5
2.3. Identifiers and keywords from The Python Language Reference:
Identifiers are unlimited in length.
But you'll be violating PEP-8 most likely, which is not really cool:
Limit all lines to a maximum of 79 characters.
Also you'll be violating PEP-20 (the Zen of Python):
Readability counts.
17 Comments
AggregateAccountabilityViewManager<AlternateSetupScrollingLoaderView> aggregateAccountabilityViewManagerForAlternateSetupScrollingLoaderViews = new AggregateAccountabilityViewManager<AlternateSetupScrollingLoaderView>(); (as a random Java example) in code. Just because code-completion makes stuff like that a lot easier to write doesn't mean you should.They could be a problem for the programmer. Keep the function names reasonably short, and use docstrings to document them.
Comments
Since attribute names just get hashed and turned in to keys on inst.__dict__ for 99% of classes you'll ever encounter, there's no real limit on length. As long as it is hashable, it'll work as an attribute name. For the other 1% of classes that fiddle with __setattr__\ __getattr__\ __getattribute__ in ways that break the guarantee that anything hashable is a valid attribute name though, the previous does not apply.
Of course, as others have pointed out, you will have code style and quality concerns with longer named attributes. If you are finding yourself needing such long names, it's likely indicative of a design flaw in your program, and you should probably look at giving your data more hierarchical structure and better abstracting and dividing responsibility in your functions and methods.
9 Comments
the interpreter doesnt hash method names on each invocation, see ...setattr does utilize interning for Python strings. hg.python.org/cpython/file/tip/Objects/object.c#l953 PEP-3131 says that there is no limit to the identifier length. But a specific implementation of Python may decide to limit this length.
Comments
Actually there is kind of a limit:
size = 1
while True:
print(size)
name = 'a' * size
exec(f'{name} = 1')
exec(f'del {name}')
size *= 2
Output:
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
Segmentation fault (core dumped)