Advanced Python (Fall 2017)/lecture2

From ICO wiki
Revision as of 20:04, 16 September 2017 by Eroman (talk | contribs) (Created page with "= Lecture 2 = == Scope and style == Python scope is similar to other programming language and does not have many suprises. Basic scoping rules can be found here: https://en....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Lecture 2

Scope and style

Python scope is similar to other programming language and does not have many suprises. Basic scoping rules can be found here: https://en.wikipedia.org/wiki/Scope_(computer_science)#Python

my_variable = 1  # in the global scope

def my_function():
    print(my_variable)


my_function()  # prints 1
print(my_variable)  # prints 1

variables in the global scope are accessible in the local scope of a function

def my_function():
    my_variable = 2  # in the function scope
    print(my_variable)


my_function()  # prints 2
print(my_variable)  # raises NameError

variables defined in the local scope of a function are not available outside the function

my_variable = 1  # in the global scope

def my_function():
    my_variable = 2  # in the function scope
    print(my_variable)


my_function()  # prints 2
print(my_variable)  # prints 1

functions have there own scope and assignment statement in variables (i.e. my_variable = 2 creates a new variable rather than using a variable in the global scope.

my_variable = 1  # in the global scope

def my_function():
    global my_variable
    my_variable = 2  # in the function scope
    print(my_variable)


my_function()  # prints 2
print(my_variable)  # prints 1

To modify a variable in the global scope from inside a function, you can use the global keyword. NB! Modifying global variables if bad practice and should not be done in an application you write!

Task 1

Replace the use of global with something better.

x = 0

def add_1():
    global x
    x += 1

add_1()
add_1()
add_1()

print(x)  # prints 3

possible solution

class MyCounter:
    def __init__(self, x):
        self.x = x

    def add_1(self):
        self.x += 1


my_counter = MyCounter(0)
my_counter.add_1()
my_counter.add_1()
my_counter.add_1()
print(my_counter.x)

Python Packaging (Part 1)

We will do another lesson on this.

Step 1 make a git repository

This is not required to make a python package. You can make a python package without git, but we will use git in this example.

In github, make a repositiory and clone the empty repository. Make sure there is a .gitignore with appropriate rules for python.

Step 2 add some necessary files

To make a python package, you need a python module and a file called setup.py. setup.py can start with this:

from distutils.core import setup

setup()

the function setup then takes keyword arguments that describe our project.

Now make a python module. I will use a module called example. So I will make a directory called example and put an empty file called __init__.py in the directory example

to make a file that will execute when we call this package from the command line, then create a file called __main__.py

step 3 add some code

you can add these keyword arguments to setup in setup.py

from distutils.core import setup

setup(
    name='firstpackage',
    version='0.1',
    packages=['example'],
)

packages should have the name of your python module in it.

add something to execute in __main__.py like:

print('Hi from my first package')

Using your new python package

You can build it with python setup.py sdist. This will create an archive in a newly created dist directory you can install it with pip install dist/firstpackage-0.1.tar.gz and you can run it with python -m example. now you will see 'Hi from my first package' printed in the terminal!