Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

python - How should I name my classes and function and even strings?

I am new to Python as you might tell. I have read various documents but I still can not figure out if there's a "naming best practices" for strings functions and of course, classes.

If I want to name a class or a function as a SiteMap, is it ok to use SiteMap? Should it be Site_map or any other thing, for example?

Thank you!

PS. any further reading resource is GREATLY appreciated! PS. I am doing web-app development (learning, better to say!)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Naming Conventions:

There are various Python naming conventions I use. Consistency here is certainly good as it helps to identify what sort of object names point to. I think the conventions I use basically follow PEP8.

1) Module names should be lowercase with underscores instead of spaces. (And should be valid module names for importing.)

2) Variable names and function/method names should also be lowercase with underscores to separate words.

3) Class names should be CamelCase (uppercase letter to start with, words run together, each starting with an uppercase letter).

4) Module constants should be all uppercase.

E.g. You would typically have module.ClassName.method_name.

5) Module names in CamelCase with a main class name identical to the module name are annoying. (e.g. ConfigParser.ConfigParser, which should always be spelt configobj.ConfigObj.)

6) Also, variables, functions, methods and classes which aren't part of your public API, should begin with a single underscore. (using double underscores to make attributes private almost always turns out to be a mistake - especially for testability.)

Whitespace

And finally, you should always have whitespace around operators and after punctuation. The exception is default arguments to methods and functions.

E.g. def function(default=argument): and x = a * b + c


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...