Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Write Better Python Cleaner Code PEP 8

I've seen plenty of people using Python use camelCase, but should you use snake_case?

I am starting to like camelCase (with the very first letter lowercased) more then snake_case because it's faster to type. However, Kenneth Love says that it's better to use snake_case for variable names, function names, file names, etc. and CamelCase (with first letter uppercased) for class names.

Should I use camelCase instead of snake_case? Is this conventional or is it up to you to choose?

Any help appreciated.

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Straight from the creators of Python, naming conventions. You should only use StudlyCaps for class names. Constants should be IN_ALL_CAPS with underscores separating words. Variable, method, and function names should always be snake_case (wonder where it got that name?). You'll see camelCase in some really old Python code but it's not the modern convention at all.

Write your code according to the conventions so other Python developers will know what kind of construct they're dealing with when reading your code. Your code will also fit in better with open source projects or companies that work you with.

OK thanks! I'll stick with snake_case.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

No, stick with what's appropriate for the type of name :D

If it's a class, use StudlyCaps. If it's a constant, use ALL_CAPS. Etc etc.

That's basically what I meant :laughing: