<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Advanced_Python_%28Fall_2017%29%2Flecture2</id>
	<title>Advanced Python (Fall 2017)/lecture2 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Advanced_Python_%28Fall_2017%29%2Flecture2"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture2&amp;action=history"/>
	<updated>2026-06-16T02:39:57Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture2&amp;diff=124198&amp;oldid=prev</id>
		<title>Eroman: Created page with &quot;= 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....&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture2&amp;diff=124198&amp;oldid=prev"/>
		<updated>2017-09-16T17:04:20Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= 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....&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Lecture 2 =&lt;br /&gt;
&lt;br /&gt;
== Scope and style ==&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;my_variable = 1  # in the global scope&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    print(my_variable)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
my_function()  # prints 1&lt;br /&gt;
print(my_variable)  # prints 1&amp;lt;/source&amp;gt;&lt;br /&gt;
variables in the global scope are accessible in the local scope of a function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def my_function():&lt;br /&gt;
    my_variable = 2  # in the function scope&lt;br /&gt;
    print(my_variable)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
my_function()  # prints 2&lt;br /&gt;
print(my_variable)  # raises NameError&amp;lt;/source&amp;gt;&lt;br /&gt;
variables defined in the local scope of a function are not available outside the function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;my_variable = 1  # in the global scope&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    my_variable = 2  # in the function scope&lt;br /&gt;
    print(my_variable)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
my_function()  # prints 2&lt;br /&gt;
print(my_variable)  # prints 1&amp;lt;/source&amp;gt;&lt;br /&gt;
functions have there own scope and assignment statement in variables (i.e. &amp;lt;code&amp;gt;my_variable = 2&amp;lt;/code&amp;gt; creates a new variable rather than using a variable in the global scope.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;my_variable = 1  # in the global scope&lt;br /&gt;
&lt;br /&gt;
def my_function():&lt;br /&gt;
    global my_variable&lt;br /&gt;
    my_variable = 2  # in the function scope&lt;br /&gt;
    print(my_variable)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
my_function()  # prints 2&lt;br /&gt;
print(my_variable)  # prints 1&amp;lt;/source&amp;gt;&lt;br /&gt;
To modify a variable in the global scope from inside a function, you can use the &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; keyword. NB! Modifying global variables if bad practice and should not be done in an application you write!&lt;br /&gt;
&lt;br /&gt;
=== Task 1 ===&lt;br /&gt;
&lt;br /&gt;
Replace the use of global with something better.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;x = 0&lt;br /&gt;
&lt;br /&gt;
def add_1():&lt;br /&gt;
    global x&lt;br /&gt;
    x += 1&lt;br /&gt;
&lt;br /&gt;
add_1()&lt;br /&gt;
add_1()&lt;br /&gt;
add_1()&lt;br /&gt;
&lt;br /&gt;
print(x)  # prints 3&amp;lt;/source&amp;gt;&lt;br /&gt;
possible solution&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;class MyCounter:&lt;br /&gt;
    def __init__(self, x):&lt;br /&gt;
        self.x = x&lt;br /&gt;
&lt;br /&gt;
    def add_1(self):&lt;br /&gt;
        self.x += 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
my_counter = MyCounter(0)&lt;br /&gt;
my_counter.add_1()&lt;br /&gt;
my_counter.add_1()&lt;br /&gt;
my_counter.add_1()&lt;br /&gt;
print(my_counter.x)&amp;lt;/source&amp;gt;&lt;br /&gt;
== Python Packaging (Part 1) ==&lt;br /&gt;
&lt;br /&gt;
We will do another lesson on this.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 make a git repository ===&lt;br /&gt;
&lt;br /&gt;
This is not required to make a python package. You can make a python package without &amp;lt;code&amp;gt;git&amp;lt;/code&amp;gt;, but we will use &amp;lt;code&amp;gt;git&amp;lt;/code&amp;gt; in this example.&lt;br /&gt;
&lt;br /&gt;
In github, make a repositiory and clone the empty repository. Make sure there is a &amp;lt;code&amp;gt;.gitignore&amp;lt;/code&amp;gt; with appropriate rules for python.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 add some necessary files ===&lt;br /&gt;
&lt;br /&gt;
To make a python package, you need a python module and a file called &amp;lt;code&amp;gt;setup.py&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;setup.py&amp;lt;/code&amp;gt; can start with this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;from distutils.core import setup&lt;br /&gt;
&lt;br /&gt;
setup()&amp;lt;/source&amp;gt;&lt;br /&gt;
the function &amp;lt;code&amp;gt;setup&amp;lt;/code&amp;gt; then takes keyword arguments that describe our project.&lt;br /&gt;
&lt;br /&gt;
Now make a python module. I will use a module called &amp;lt;code&amp;gt;example&amp;lt;/code&amp;gt;. So I will make a directory called &amp;lt;code&amp;gt;example&amp;lt;/code&amp;gt; and put an empty file called &amp;lt;code&amp;gt;__init__.py&amp;lt;/code&amp;gt; in the directory &amp;lt;code&amp;gt;example&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to make a file that will execute when we call this package from the command line, then create a file called &amp;lt;code&amp;gt;__main__.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== step 3 add some code ===&lt;br /&gt;
&lt;br /&gt;
you can add these keyword arguments to &amp;lt;code&amp;gt;setup&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;setup.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;from distutils.core import setup&lt;br /&gt;
&lt;br /&gt;
setup(&lt;br /&gt;
    name=&amp;#039;firstpackage&amp;#039;,&lt;br /&gt;
    version=&amp;#039;0.1&amp;#039;,&lt;br /&gt;
    packages=[&amp;#039;example&amp;#039;],&lt;br /&gt;
)&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;packages&amp;lt;/code&amp;gt; should have the name of your python module in it.&lt;br /&gt;
&lt;br /&gt;
add something to execute in &amp;lt;code&amp;gt;__main__.py&amp;lt;/code&amp;gt; like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;print(&amp;#039;Hi from my first package&amp;#039;)&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Using your new python package ===&lt;br /&gt;
&lt;br /&gt;
You can build it with &amp;lt;code&amp;gt;python setup.py sdist&amp;lt;/code&amp;gt;. This will create an archive in a newly created &amp;lt;code&amp;gt;dist&amp;lt;/code&amp;gt; directory you can install it with &amp;lt;code&amp;gt;pip install dist/firstpackage-0.1.tar.gz&amp;lt;/code&amp;gt; and you can run it with &amp;lt;code&amp;gt;python -m example&amp;lt;/code&amp;gt;. now you will see &amp;#039;Hi from my first package&amp;#039; printed in the terminal!&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>