I'm Learning Python part 10
<p>
<h1 style="font-size:18pt;font-family:Verdana;">
(last one)
</h1>
<p>
<img src="http://www.python.org/images/python-logo.gif" alt="Python Logo" title="Python Logo" /> </div>
<h1 style="font-family:Georgia;">
Back to blogging
</h1>
<p>
As usual, I will apologize for not blogging for a long time.<br /> I have been very busy, university exams, university projects, job projects, teaching and learning.
</p>
<h1 style="font-family:Georgia;">
Python Course
</h1>
<p>
At Damascus University, in the faculty of informatics we managed to create free courses to students, and I was one of the teachers there, I taught Python to students.<br /> As far as I know this course was the first Python course in Damascus University.<br /> Even though the students were a few (actually a very little few about 8 ~ 10 students) the course was great. We managed to learn Python 2.6 Syntax, a little bit of its standard library and a little bit of PyQt4 in about 7 days x 2 hours daily.<br /> As far as I know too, students understood it and found it great, and I hope they’ll be using this great language more in their programs.
</p>
<h1 style="font-family:Georgia;">
Why last one?
</h1>
<p>
The tour with Python ends here, while it ends here it starts here too, it ends here because so far you’ve learned what you need to start your own path in Python. And it starts here because you’re fully equipped with the base tool to discover more tools, I’ll let you discover the standard library and 3rd-party libraries on your own, because everyone differs in his/her interests.<br /> I’ll be blogging more on more technical issues but they might not be I’m Learning Python series :).<br /> Let’s stop talking here and move directly to the heart of our last lesson.<br />
<h1 style="font-family:Georgia;">
Revision
</h1>
<p>
The last part introduced classes, how to define them, how to use them. This lesson will continue to explain classes and functions more.<br /> Defining a function:
</p>
<pre class="lang:python decode:true">
def functionName(param0, param1, ...):
#Your function code goes here.
#You can return values using:
return values
<p>
Defining a class:
</p>
<pre class="lang:python decode:true">
class ClassName(BaseClass0, BaseClass1, ...):
#Your class code goes here.
#Your class ends whenever you go out of the scope (you go back one tab).
<p>
The constructor of the class is defined using the special name __init__:
</p>
<pre class="lang:python decode:true">
class A:
def __init__(self, param0, ...):
#Constructor code goes here.
<p>
The method inside a class is just a function which receives the object instance as its first parameter:
</p>
<pre class="lang:python decode:true">
class A:
def myMethod(self, param0, ...):
#method is just a function
<p>
Properties are fields encapsulated within a function to get their values and might be encapsulated withing a function to set their values. These two functions allow you to do your business logic over the values before allowing the user to read/write them.
</p>
<pre class="lang:python decode:true">
class A:
@property
def Id(self):
return self.__id
@Id.setter
def setId(self, value):
self.__id = value
def __init__(self):
self.__id = 0
<p>
Defining an object instance of the class is done by calling the class constructor:
</p>
<pre class="lang:python decode:true">
a = A()
<p>
If the constructor takes a list of parameters you <b>might</b> specify them when calling it:<br />:
</p>
<pre class="lang:python decode:true">
class B:
def __init__(self, name):
self.name = name
b = B('some name')
<p>
Properties after defining an object instance can be used as if they were fields:
</p>
<pre class="lang:python decode:true">
a.Id = 10
print a.Id
<h1 style="font-family:Georgia;">
More and more about functions
</h1>
<p>
Functions can be used in many ways.
</p>
<h2>
Functions with default values
</h2>
<p>
You can let the parameters of the functions have default values, so that if the user didn’t supply them they’ll have valid values:
</p>
<pre class="lang:python decode:true">
def f(id = 1, name = 'some name', age = 10):
print(str(id) + ' ' + name + ' at age ' + str(age))
#All of the following calls are valid
f() #Will print: 1 some name at age 10
f(2) #Will print: 2 some name at age 10
f(2, 'a') #Will print: 2 a at age 10
f(2, 'a', 3) #Will print: 2 a at age 3
<p>
What if I want to supply the second paramter without supplying the first one and the third one? The solution is by:
</p>
<h2>
Passing specified parameters
</h2>
<p>
You can choose the paramters you want to supply and with any order you want:
</p>
<pre class="lang:python decode:true">
#All of the following calls are valid
f(age = 3) #Wil print: 1 some name at age 3
f(age = 3, id = 2) #Will print: 2 some name at age 3
f(name = 'a', age = 3, id = 2) #Will print: 2 a at age 3
<p>
You can see that this way is useful, you can choose which parameters to pass and in what order :).
</p>
<h2>
Passing unlimited count of paramters
</h2>
<p>
Yes you can do it, you can call a function and pass it any count of parameters you want:
</p>
<pre class="lang:python decode:true">
def u(*args):
s = ''
for arg in args:
s += arg + ' '
print s
#All of the following calls are valid
u() #Will print nothing
u(1) #Will print: 1
u(1, 2) #Will print: 1 2
u(1, 2, 3) #Will print: 1 2 3
<p>
So args actually is enumerable type, may be a list ;).
</p>
<h2>
Passing unlimited named parameters
</h2>
<p>
Hmmmm, yes you can too specify names for those unlimited parameters of yours:
</p>
<pre class="lang:python decode:true">
def uu(**kargs):
s = ''
for k, v in kargs.iteritems():
s += k + ' => ' + v + ', '
print s
#All of the following calls are valid
uu() #Will print nothing
uu(a = 1) #Will print: a => 1
uu(a = 1, b = 2) #Will print: a => 1, b => 2,
uu(a = 1, b = 'asd', c = 0.5) #Will print: a => 1, b => 'asd', c => 0.5,
<p>
So kargs actually is a dictionary.<br /> If you don’t already know, dictionary.iteritems is a method that iterates over the keys and values of the dictionary, yielding a tuple every iteration, the first item of the tuple is the key, and the other is the value.<br /> dictionary.iteritems is a great method for iterating over a dictionary of unknown keys.
</p>
<h2>
Overloading and why I don’t need it here
</h2>
<p>
Overloading is simply more than one instance of the same function with one or more of the following points achieved in them:<br /> * The count of parameters differs between them.<br /> * The types of the parameters differ between them.<br /> * The prder of the parameters differ between them.
</p>
<p>
Using what we’ve learned so far, we can say that the first point can be achieved using one function, by using default values for the parameters.<br /> The second point can also be achieved using one function since Python has no strict data types to variables and parameters.<br /> The third point can also be achieved using one function, by using paramters names while calling you can change the order of parameters passed.<br /> And so I can continue coding without overloading.
</p>
<h1 style="font-family:Georgia;">
Last Words
</h1>
<p>
After learning how to implement classes and functions properly you can start your path on learning Python standard library and 3rd-party libraries. Python is widely supported and you won’t find difficulties in using it as your main programming language.<br /> In the last two months I learned how to use Python to develop web applications using Django (pronounced yangoo), and being an expert ASP .NET developer I assure you, Django is easier and more fun to code than ASP .NET. I made a testing website from the book I read – The book was: <a href="http://www.packtpub.com/django-1-0-website-development-2nd-edition">Django 1.0 Website Development</a>, by my colleague and friend <a href="http://www.aymanh.com">Ayman Hourieh</a> -, the website is for sharing bookmarks, it is now hosted for free at AlwaysData here <a href="http://mpcabd.alwaysdata.net">http://mpcabd.alwaysdata.net</a><br /> You can do in Python whatever you want with less coding. Enjoy your life more.
</p>
<h1 style="font-family:Georgia;">
The End
</h1>
<p>
It is the end of this course but the start of your path.<br /> I hope you enjoyed it, and that I could give you the information in a nice and easy way.<br /> For any questions or suggestions you can leave your comments here.<br /> Thanks for being patient :).<br /> — Abd Allah Diab </div>