20

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?

asked Jun 4, 2013 at 14:46
1
  • 6
    Please 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. Commented Jun 4, 2013 at 15:20

5 Answers 5

45

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.

answered Jun 4, 2013 at 14:48
Sign up to request clarification or add additional context in comments.

17 Comments

@AlexanderTobiasHeinrich Your code will be absolutely unreadable. Use docstrings.
"Fully descriptive" and "readable" are two separate things. Long, long camel-cased strings are the former, not the latter.
@chepner Indeed. I hate it when I encounter things like 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.
@AlexanderTobiasHeinrich Because functionsThatTellYouExactlyWhatTheyDoInCamelCase(areReallyAnnoyingBecauseItAllRunsTogether).
@chepner: I love comments, if they document a public interface. If they don't I'd rather try to write code that does not need comments.
|
7

They could be a problem for the programmer. Keep the function names reasonably short, and use docstrings to document them.

answered Jun 4, 2013 at 14:48

Comments

3

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.

answered Jun 4, 2013 at 14:51

9 Comments

You really think that interpreter hashes method names on each invocation?
Would be more productive if worded the interpreter doesnt hash method names on each invocation, see ...
Unless you are using a JIT or other sort of optimizing or look-ahead interpreter (which to my knowledge CPython is not), yes.
Not dumb at all. My understanding is that the CPython interpreter was specifically designed to be as simple to extend as possible, which meant tradeoffs for performance optimization. Hence why PyPy and the like exist.
@delnan It seems they are interned... when using a char* string. hg.python.org/cpython/file/tip/Objects/object.c#l816 On the other hand, setattr does utilize interning for Python strings. hg.python.org/cpython/file/tip/Objects/object.c#l953
|
1

PEP-3131 says that there is no limit to the identifier length. But a specific implementation of Python may decide to limit this length.

answered Dec 17, 2023 at 17:23

Comments

0

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)
answered Jan 8, 2025 at 18:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.