- Home
- Contact
-
Articles / Code
- HTML/CSS (2)
-
Scripts (17)
- Twitter & bash
- Generate sitemaps
- random floating point in bash
- Gmail script
- Python http POST requests
- Bashrc enhancements
- commandline <> nautilus
- Word Definitions
- Synonyms
- mysqld monitor
- remote server
- links and emailaddresses
- Apache Analyser
- remote ipaddress
- OOP Python
- mysql tuning
- diskspace notification
- Server Configuration (6)
- ICT-security (2)
Introduction to Object Oriented Programming in Python
» Articles / Code » Scripts » Introduction to Object Oriented Programming in Python
There is a lot that can be said about Python & OOP, classes , etc. but lets keep
it simple.
A class is simply a collection of functions and data. This grouped
together makes it easier to work with; you can call the 'group' (class)
instead of each individual member.
This example shows an empty class 'Group' and some data about a 'group' of students
class Group:
pass
my_group = Group()
my_group.students = 11
my_group.assistants = 3
my_group.roomnumber = "23a"
'pass' means "do nothing" and is required if the block would otherwise be empty.
First we create an instance and call it my_group; it doesn't have any
atributes yet (it's empty) so, 3 atributes and their data were added
(students, assistant and roomnumber).
getting the data is easy:
print my_group.my_students """ 11 """
print my_group.assistants """ 3"""
Now, that's easy, but not really interesting. Our 'Group' is not really
a class yet.
Lets add the attributes to the class, so they will be created when the
object is instantiated:
class Group:
def __init__(self, students, assistants, roomnumber):
self.students = students
self.assistants = assistants
self.roomnumber = roomnumber
That's it! Once we make an instance of this 'Group', it will have 3
attributes (although they don't ave any data in it yet).
We can make in instance and give it some data.
my_group = Group(11, 3 , 23)
print my_group.students """11"""
print my_group.assistant """3"""
Because the class has an __init__ method, it's automatically called when an instance is created.
Note that __init__ is defined with an extra first argument: 'self'.
But we don't specify 'self' when we call the method. All Python methods work like this. 'self' is in fact the instance itself, and Python supplies it behind the scenes.
You need 'self' because it's the only way the method can access the instance's attributes and other methods.
So far, the class contains only one method __init__() that adds a few attributes.
Adding methods to this class
lets say we add a method (function) to this class that returns the total of all people (students + assistant).
it could be something like
#!/usr/bin/python
class Group:
def __init__(self, students, assistants, roomnumber):
self.students = students
self.assistants = assistants
self.roomnumber = roomnumber
def total_ppl(self):
return self.students + self.assistants
my_group = Group(22, 4, "12a")
print my_group.total_ppl()
If you are used to OO in other languages, than you might wonder about things like 'Private', 'Public', 'constructors' and 'Function definitions outside the class definition'.
For now, don't worry about it... I could say some things about these, but Python simply takes care of most of these things automagically. It's as simple as that...
Post your comment
Comments
No one has commented on this page yet.
RSS feed for comments on this page | RSS feed for all comments