Python Tutorial: Classes, Objects, Methods, init and Simple Examples

Python Tutorial: Classes, Objects, Methods, init and Simple Examples

Python1

Python Tutorial: Classes, Objects, Methods, init and Simple Examples

Python Tutorial on Classes, Objects, Methods: In this post we will dive into Python classes, methods, objects and using the init constructer to initialize classes. Object oriented programming has gone well past being a fad (sorry punchcards) like it or not encapsulation and inheritance is winning out. The idea is to make modules and procedures abstracted, modifiable and reusable without needing to worry about reinventing the wheel. All interesting stuff, but this is just to give some examples as I am knocking off years of rust and having some fun with Python. These are just simple examples that might help folks out as they start learning programming. Most attempts Python tutorials on classes object methods etc, are more advanced than are appropriate for beginners.


If you want a quick review on basic functions check this tutorial.


Python Classes and Object Oriented Programming OOB

Kitty writing code
If you look at python.org docs the definition is about as clear as mud. No offense intended, programming concepts in general can fairly nacient and wide open to interpretation regardless of language since it is as much style and theory. I tend to explain classes and methods (think function in a class) as a blueprint for what to pass and object data with attributes being applied through a framework. I can pass an object (examples to follow) into a class and it modifies the object based on the blueprint.

Python Methods

In the simplest term a method is a function inside of an object. You can pass attributes (variable inside a method) to the class and method that will get processed inside of the method and independently for each object. Variables or attributes defined or initialized at the beginning of the class are inherited to the methods inside that class. We will see in the examples.

Python Self for OOB

This can be rather confusing starting out. In a method which is a function inside of a class the first parameter is always self. Self is just referring to the object you passed into the method. Clear as mud? One other thing is a class object can be called or instantiated using __init__ (self). Using init will save you a couple lines of code. I learn through example so enough chit chat.

Practical Application (ever so loosely o_O)

A practical application of this could be calling a process that would spawn a VM to spin up. Here we will just print the name but you could execute a shell command that would spawn a widget.

Class and Method Examples

So here are a couple of easy peasy examples that should help you get a kickstart, think of code reusability and as why you would add this extra complexity. Also you notice what happens when we try and print from the method, it returns ‘none’, in the next example we will change that to return which can be useful to break out of methods and also return parameters back from the method while pass would be indicate and empty block statement:

The output looks like this:

Return Statement from a Function or Method in a Class

Let’s add the ‘return‘ statement and add a bit of formatting. Return allows us to use the method data from the class outside of the class. I also added a couple of different ways to print strings in there for reference:

The output looks like this:

Constructors the Magical Method __init__ Example

When a instance of a class is created the initializer is called with __init__, this can set default values and can save you from explicitly initializing a class with something like “firstHost = VMname()” in our previous example. C+ constructors are a similar concept. Clear as mud!

Here is the exciting output and names of our hosts!

That does it for this one. Excuse mistakes and bad concepts, a lot of this can be open to interpretation or just flat wrong but the examples are what I need mostly so hopefully those will help those visiting Python or knocking off the rust. More Tutorials in the Programming section.



Thanks for stopping by.


About the Author

Brent SalisburyI have over 20 years of experience wearing various hats from, network engineer, architect, ops and software engineer. More at Brent's LinkedInView all posts by Brent Salisbury →

  1. Ciprian TomoiagaCiprian Tomoiaga02-23-2013


    Thank you so much! Such a simple and easy way of putting it. Well done. Thanks!

  2. Brent SalisburyBrent Salisbury02-25-2013


    Thanks Ciprian!

    Respect,
    -Brent

  3. Jason ParragaJason Parraga06-07-2013


    Was looking for a basic python tutorial since I am full retard when it comes to OOP python. It’s amazing i found this…

  4. kirillkirill02-12-2014


    Thanks man, took a while to find but helped alot

  5. colbertcolbert02-15-2014


    Thanks very much It help me a lot. I have been looking for an article like this until I found it today.