浏览代码

Merged README

Fred Damstra 6 年之前
父节点
当前提交
232348c23d
共有 4 个文件被更改,包括 83 次插入3 次删除
  1. 22 0
      README.md
  2. 24 0
      examples/methodtypes.py
  3. 34 0
      examples/module_skel.py
  4. 3 3
      examples/simplefunction.py

+ 22 - 0
README.md

@@ -2,6 +2,13 @@
 
 Just notes on various things
 
+## Naming Notes
+Classes use CamelCaps
+Functions and varibles are lowercase() or lowercase_with_underscores().
+
+Always use self for the first argument to instance methods.
+Always use cls for the first argument to class methods
+
 ## Magic Methods
 Classes have certain magic methods that help make objects behave in pythonlike ways.
 
@@ -9,8 +16,23 @@ Classes have certain magic methods that help make objects behave in pythonlike w
 * `__str__` returns a string representation of the object. Should be easily readable but not necessarily
   unambiguous. Careful, can be confused with `__repr__` in some cases. 
 
+## Comments
+Block comments are at the same indentation and are complete sentences.
+Inline comments shouldn't state the obvious.
+
+Avoid:
+```
+x = x + 1  # Increment x
+```
+
+But this can be useful:
+```
+x = x + 1  # Compensate for the border
+```
+
 ## docstrings
 Use 'em.
+See PEP257 - https://www.python.org/dev/peps/pep-0257
 
 ### doctest
 In your docstring, put examples that look like the command-line, such as

+ 24 - 0
examples/methodtypes.py

@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+""" Demonstrates the difference between an instance method, 
+    a class method, and a static method.
+"""
+
+class MyClass:
+    # Instance method. Have to instantiate an instance
+    # first! And it can change all the stuff that is
+    # unique to that instance. (It can also change
+    # the class itself, if it wants to, by accessing
+    # self.__class__
+    def method(self):
+        return 'instance method called', self
+
+    # The class method doesn't need an object to be 
+    # instantiated. It just is.
+    @classmethod
+    def classmethod(cls):
+        return 'class method called', cls
+
+    # Static methods don't even get access to the class
+    @staticmethod
+    def staticmethod():
+        return 'static method called'

+ 34 - 0
examples/module_skel.py

@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+"""
+This includes some skeleton stuff for a module
+using python 3, using some guidelines from PEP8.
+"""
+# From future imports go here
+from __future__ import better_implementation
+
+# Then dunders
+__all__ = ['dont', 'use', 'all']
+__version__ = 0.1
+__author__ = "Fred Damstra, Other Guy, Another Guy"
+__copyright__ = "Copyright 2018, MonkeyBOX Entertainment"
+__credits__ = ["Fred Damstra", "Other Guy", "Another Guy"]
+__license__ = "GPL"
+__maintainer__ = "Fred Damstra"
+__email__ = "fred.damstra@gmail.com"
+__status__ = "Prototype" # Either Prototype, Development, or Production
+
+# This is the standard order of imports. The comments
+# should probably be removed but the line breaks should
+# remain between groups.
+#
+# Standard library imports.
+import os
+import sys
+
+# Related third party imports.
+import gitlib
+
+# Local application/library specific imports.
+from mylib import myfunc
+
+

+ 3 - 3
examples/simplefunction.py

@@ -5,7 +5,7 @@ def example(firstargument, secondargument, thirdargument="N/A"):
     """ Prints a string based on the two arguments. """
     # This is python3.6 syntax:
 #    print(f"{firstargument} is the first argument.")
-    print("{a} is the first argument.".format(a = firstargument)) # Name the argument if you want
+    print("{a} is the first argument.".format(a=firstargument)) # Name the argument if you want
     print("{} is the second argument.".format(secondargument))
     print("{} is the third argument.".format(thirdargument))
 
@@ -18,6 +18,6 @@ if __name__ == "__main__":
     """
     # You can just use parameters in order
     example("My first argument", "my second argument")
-    # but better to use the names
-    example(secondargument = "#2!", firstargument = "#1!", thirdargument = "#3!")
+    # but better to use the names, without spaces here!
+    example(secondargument="#2!", firstargument="#1!", thirdargument="#3!")