Skip to content

Instantly share code, notes, and snippets.

@robmccormack
Last active December 20, 2015 22:39
Show Gist options
  • Save robmccormack/6207123 to your computer and use it in GitHub Desktop.
Save robmccormack/6207123 to your computer and use it in GitHub Desktop.
Python Tips - by Rob.

Python Tips

A test link plugin for creating and editing Gists.

Not to do..

Do not use triple-quote strings to comment code. This is not a good practice, becaus https://python-guide.readthedocs.org/en/latest/writing/documentation/#project-documentation

###Line wrapping, comment

In Python, docstrings describe modules, classes, and functions:

def square_and_rooter(x):
    """Returns the square root of self times self."""
    ...

##TIPS: http://www.nilunder.com/blog/2013/08/03/pythonic-sensibilities/

# Yes:
class Something:
    
    def __init__(self):
        self.extra = [2, 1, 3]

        # Public:
        self.elements = []
        self.elements_parallel = []
    
    def spice(self):
        return sum(self.extra) / 3

    # Public:
    def speak(self):
        print(sum(self.elements) + self.spice())
    
    def speak_parallel(self):
        print(sum(self.elements_parallel) + self.spice())

Ref: not just python. Getting into the habit of writing code that is always less than 79 characters is a good thing and you should be doing it. Here's why ... https://jamiecurle.co.uk/blog/79-characters-or-less/

The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72). http://www.python.org/dev/peps/pep-0008/ The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:

Samples:

with open('/path/to/some/file/you/want/to/read') as file_1, \
        open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())

note, the backslash is continue to next line. This tip from excellent 10 min. tutorial http://www.stavros.io/tutorials/python/

>>>print "Name: %s\
Number: %s\
String: %s" % (myclass.name, 3, 3 * "-")
Name: Poromenos
Number: 3
String: ---

strString = """This is
a multiline
string."""

# WARNING: Watch out for the trailing s in "%(key)s".
>>> print "This %(verb)s a %(noun)s." % {"noun": "test", "verb": "is"}
This is a test.

the %s means string, and has special meaning, %d is an interger http://www.tutorialspoint.com/python/python_strings.htm

print "My name is %s and weight is %d kg!" % ('Zara', 21) 

When the above code is executed, it produces following result:

My name is Zara and weight is 21 kg!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment