Python anatomy: Difference between revisions

From wikinotes
 
No edit summary
 
Line 17: Line 17:
See https://www.python.org/dev/peps/
See https://www.python.org/dev/peps/


In particular PEP8 is the current style-guide used by the python project itself, and  
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/
is used by most python projects: https://www.python.org/dev/peps/pep-0008/
</blockquote><!-- Best Practices -->
</blockquote><!-- Best Practices -->


= Example Sourcefile =
= Sample Sourcefile =
<blockquote>
<blockquote>
<source lang="python">
<source lang="python">
Line 38: Line 38:
         Args:
         Args:
             text (str):  text you'd like to capitalize
             text (str):  text you'd like to capitalize
               
 
         Returns:
         Returns:
             str: Capitalized text.
             str: Capitalized text.
Line 57: Line 57:
     instance.capitalize('hi there')
     instance.capitalize('hi there')
</source>
</source>
</blockquote><!-- Example Sourcefile -->
= Sample Project =
<blockquote>
{{ TODO |
this
}}


</blockquote><!-- Example Sourcefile -->
 
</blockquote><!-- Sample Project -->

Latest revision as of 23:07, 14 February 2024

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