Classes and Functions and Threading Oh My! - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32) +---- Thread: Classes and Functions and Threading Oh My! (/showthread.php?tid=23637) |
Classes and Functions and Threading Oh My! - Rhynorater - 12-06-2011 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(): 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): 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): 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: 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 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 RE: Classes and Functions and Threading Oh My! - BreShiE - 12-06-2011 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. RE: Classes and Functions and Threading Oh My! - Rhynorater - 12-08-2011 (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 ;) RE: Classes and Functions and Threading Oh My! - Caffeine - 12-14-2011 This is a great guide. Thanks for posting this! RE: Classes and Functions and Threading Oh My! - Rhynorater - 12-17-2011 (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 |