Python anatomy

From wikinotes
Revision as of 23:07, 14 February 2024 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

python is pretty straightforwards.


Language Semantics

  • dynamically typed
  • 1st class objects

Best Practices

Python releases styleguides with best practices in the form of PEPs. (these are also used to propose changes to the python language). You can look them up here:

See https://www.python.org/dev/peps/

In particular PEP8 is the current style-guide used by the python project itself, and is used by most python projects: https://www.python.org/dev/peps/pep-0008/

Sample Sourcefile

class MyClass(object):
    """ A docstring
    """

    def print_stuff():
        """ Prints word 'stuff
        """
        print('print stuff')

    def capitalize(text):
        """ Capitalizes and prints 'stuff'

        Args:
            text (str):  text you'd like to capitalize

        Returns:
            str: Capitalized text.
        """
        capitalized_text = text.upper()
        return capitalized_text


def hello_world():
    print('hello world')


# this code only runs if module is run directly
# (and not when imported)
if __name__ == '__main__':
    instance = MyClass()
    instance.print_stuff()
    instance.capitalize('hi there')

Sample Project

TODO:

this