Advertisement
uopspop

Untitled

Sep 19th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. In this Unit, we learn about Python variables and the importance of creating meaningful variable names. Do some research online about recommended Python variable naming style. Try to find authoritative guidelines and summarize them in 2 or 3 bullet points (be sure to use APA citation style for outside sources). Also, provide 2 or 3 examples of good variable names and 2 or 3 examples of discouraged (although valid) variables names. Include reasons why the variable names comply or not.
  2. Be sure to respond to three of your classmates and to respond back to anyone who has responded to your post. You will also rate 3 of the initial posts of your classmates.
  3. =====================================================================
  4.  
  5. Python Variable Naming Convention
  6. In Python, there are many sorts of variables. In terms of scope, we have local and global variables. In terms of class, we have class variables and instance variables as well as arguments of class methods and arguments of instance methods. In terms of values, we have variables that we learn in chapter 2 in the textbook (Downey A., 2015).
  7. The general recommended naming conventions for Python variables are as below (Rossum G. V. & Warsaw B. & Coghlan N., 2001):
  8. • Variable names should be lowercase
  9. • If there are many words in a variable name, use underscores to separate them
  10. • Don’t use the following characters: I(like Ice), O(like Oracle), l(like lemon). They will be mistakenly regarded as the numerals one and zero.
  11. Some good examples are as below:
  12. • >>> age = 19
  13. It complies with the convention because it uses lowercase characters.
  14. • >>> customer_location = ‘taipei’
  15. It complies with the convention because it uses underscores to separate words
  16. Some bad examples are as below:
  17. • >>> Age = 19
  18. What to improve: do not use capitalized character
  19. • >>> customerLocation = ‘Taipei’
  20. What to improve: use an underscore to separate words instead of Camel case style
  21. • >>> I = ‘my name’
  22. What to improve: do not use I(like Ice), it confuses readers whether it’s a numeral one or a character ‘I’(like Ice)
  23. Aside from the general rules, I would like to share the variable convention for class variables and instance variables. It interests me a lot because I come from a Java background and Java uses a different way to limit the scope of those variables. In Java, to regulate the scope of class variables and instance variables, we have the keywords: public, private, and protected. While in Python, there are no such keywords. The way Python regulates the scope is purely by convention. That is, you can still access those variables one way or another, but you shouldn’t because the convention tells you not to do so. If you’re also interested, please find more information in the following link. I would like to discuss with you more on this. Thanks!
  24. https://radek.io/2011/07/21/private-protected-and-public-in-python/
  25.  
  26. References
  27. Downey A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). (n.p.): O'Reilly Media.
  28. Rossum G. V. & Warsaw B. & Coghlan N. (2001). PEP 8 -- Style Guide for Python Code. Retrieved from https://www.python.org/dev/peps/pep-0008/#type-variable-names
  29. words: 319
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement