Support Forums

Full Version: Classes and Functions and Threading Oh My!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all! Today RhynoTuts is bring you a tutorial on Fuctions, Classes and Threading! Oh My! I wrote this tutorial because I had some trouble understanding these when I first started Python, and I thought some of you would enjoy having it laid out all nice and simple for ya! So here it is ;)
first up is....
Functions

Understanding functions is vital to being a good Python programmer. Functions allow the user to access certain material at any point in their program. To define a function, we simply say:
Code:
def functionname():

There, we have defined our function. You can name your function anything, I just decided to name mine "functionname". So lets analyze this, "def" stands for "define", the "functionname" is the name of the function, the empty parenthesis stand for no parameters, I will talk about that later. Lastly there is the colon at the end, this will show that there is indentation on the next lines of code.

So now lets add some material to the function.

Code:
def functionname():
    print "Hello!"

functioname()

So whats new here is the 'print "Hello!"', we didnt see that before! The reason it is indented is to show that it is in that function. So when you call that function using the "functionname()", it will do what is inside of the function. So the output of the code above would be:

Code:
Hello!

Now that is just one type of function, there is one more. We now have the "returning function". I think there is another name for this but who cares about technical terms. This is what happens when you call a "returning function"

Code:
def functionname(var1, var2):
    x = var1 + var2
    return x

print functioname(2, 3)

Here we have some code outside of a function, and we have some code in a function. Those things inside of the parentheses are called "parameters", those parameters MUST be inside the parentheses every time the name of the function comes up. For example, if you do a loop back into the function you have to give it the variables again, like so.

Code:
def main(username):
    print "welcome %s, here is the secret code 'Pythonisamazing'"%username
    main(username)
    #right there ^ We call the username variable again because when you call main you have to put the parameters in.
file = open("cred.txt")    
username = raw_input("Enter your username: ")
password = raw_input("Password: ")
if file.find(username) and file.find(password) !=0:
    main(username)
else:
    exit

So there, I think I have covered the basic use of functions. Next I will move on to Classes.

Classes

Classes are used to organize functions and for the purpose of sharing variables as well. I will go over both of those today. So here we go.

To make a class simply use this syntax:
Code:
class SupportForums:

There, we made a class called "SupportForums". Now lets put some stuff in our class so I can demonstrate its basic functions.

Code:
class SupportForums:
    this = "A shared variable"
    this2 = "Another shared variable"

    def sayhello(self):
  print "Hello"

    def returnhello(self):
  return "Hello"
    def hello(self):
  print sf.returnhello()
  print sf.this
  print sf.this2
  sf.sayhello()
  #We can access all the functions and variables in the class by using the name of the function "sf" (see bellow) and a "." and then the name of the item.

sf= SupportForums()
#We here assign the name "sf" to our class.
sf.hello()

I have commented the code so you can understand what is in it. Basicly all I did was create a class "SupportForums" and put functions and variables in it. Then if you skip to the bottom of the code, I named the class "sf" and then called the "hello" inside of it using the "sf.hello()". So that is pretty simple. Classes are pretty simple if you just remember to put the name it in. Note, however, all of the functions in the class take the "self" parameter. This is because it is a child of the parent "SupportForums" or "sf". Just put the self there, and don't forget it!

This has covered the basics of classes, read it over atleast 3x, you will understand it after that.



Threading


This is the last subject that will be covered today. Threading allows the user to make the program do several different functions at once! This is very useful in making big programs and a necessity for advanced users. Some of you may want to stop here for now and come back to this later, its kind of advanced. There is a module called "thread", however this module is outdated. So today we will be using the "threading" module.

So as some of you have guessed, we will import this module by calling:
Code:
import threading

Now we can get to the good stuff, we will now make a couple of functions and then call them all at once using the threading module.

Code:
import threading,time

def say(word):
    print "Thread 1 saying " + word
    time.sleep(2)
    print "Pce from Thread 1"

def shout():
    print "Thread 2 saying PYTHON IS THE BEST!!"

word = "WORD to YOUR MOTHER"
#This is what you need to pay attention to:
thread1 = threading.Thread(target=say, args=(word))
thread2 = threading.Thread(target=shout)
thread1.start()
thread2.start()

Okay so what we did here is make to funtions. One takes a parameter and one doesnt. We made theads for those by using the "threading.Thread" function, and then we started them by using the thread name plus ".start()". This called the threads.

Threads are not really that hard to understand, but they can be slightly annoying. I thought I would through this tutorial out there because I had a hard time with threading for a long time.

Conclusion


I hope this helped all of you become better Python coders. This language is under used and under appreciated, spread the word of its awesomeness.
Thanks for reading ;)

-Rhynorater

Amazing tutorial you've got here! Bookmarking this for future reference. I did start learning Python but never went back to it, I may start learning it again after I get better at PHP.
(12-06-2011, 05:44 PM)BreShiE Wrote: [ -> ]Amazing tutorial you've got here! Bookmarking this for future reference. I did start learning Python but never went back to it, I may start learning it again after I get better at PHP.
Thanks bro glad you enjoyed it ;)

This is a great guide. Thanks for posting this!
(12-14-2011, 10:19 AM)Caffeine Wrote: [ -> ]This is a great guide. Thanks for posting this!
No problem bro, I hope this helps people understand OOP a little better Smile