Head of Digital Marketing @Bronco. Lover of Python and Data.

Introduction To The Python Class

Python Classes have been difficult to learn, the tutorials for them are either to in-depth, complicated or to simplistic that they don't give you a good feel for how you might use one, I'm going to try and hit the sweet spot, it should be noted though that I am still very much a noob when it comes to the Python class.

Just to jump straight into it, here is a class that I have written. Basically it tracks the subscription status of a user. This on it's own is fairly useless but I wanted to give a real world example (rather than just defining a pet) and simply changing this to work with a database would make it a handy . Here's what it looks like...


class Email_subscription(object):
	'''Tracks the subscription status of a user'''

	def __init__(self, name, email):
		'''Returns the user *name*, *email* and subscription status set to subscribed'''
		self.name = name
		self.email = email
		self.status = 'Subscribed'

	def subscribe(self):
		'''Subscribes the user'''
		self.status = 'Subscribed'
		return 'You have successfully subscribed'

	def unsubscribe(self):
		'''Unsubscribes the user'''
		self.status = 'Unsubscribed'
		return 'You have successfully unsubscribed'

	def change_name(self, name):
		'''Changes the users *name*'''
		self.name = name
		return 'You have successfully changed your name to {}'.format(name)

	def change_email(self, email):
		'''Changes the users *email*'''
		self.email = email
		return 'You have successfully changed your email to {}'.format(email)

So that's the class we will break it down shortly. First though lets see how you use it!

First we need to initilise the object, this is just like assigning a variable. Like so...

sub = Email_subscription('craig', 'craig.addyman@googlemail.com')

Here's an example of use and output (commented out)...


sub = Email_subscription('craig', 'craig.addyman@googlemail.com')
print sub
# 

print sub.name
# craig

print sub.email
# craig.addyman@googlemail.com

print sub.change_name('michael')
# You have successfully changed your name to michael

print sub.name
# michael

print sub.status
# Subscribed

print sub.unsubscribe()
# You have successfully unsubscribed

print sub.status
# Unsubscribed

To try to get your head around what is happening just start but writing out this code yourself and having a little play with it, often this is the best way to learn, at least that's what I have found, try to break it and then see if you can fix it again. I like to look over the code, make changes and then predict the new output.

Hopefully you've started to get the hang of it but just to explain each section...


class Email_subscription(object):
	'''Tracks the subscription status of a user'''

Here we are defining the name of the class, pretty straight-forward, if you are struggling here you will want to learn about Functions first! One note is the use of the uppercase, this is just common convention. The other bit you might have noticed is the word 'object', this is telling the class to inherit the Python defined 'object attributes', at this stage you don't need to know much more then that.


	def __init__(self, name, email):
		'''Returns the user *name*, *email* and subscription status set to subscribed'''
		self.name = name
		self.email = email
		self.status = 'Subscribed'

This next section is the __init__ method, this is short for initialise which is basically what is inisiallised when a class object is used (more on this in just a second). In this example we are using 'name' and 'email' these are just function arguments, nothing more, the 'self' argument is the trickier bit but again more on this in just a second.


	def subscribe(self):
		'''Subscribes the user'''
		self.status = 'Subscribed'
		return 'You have successfully subscribed'

Finally this last section (the other sections are just the same as this one) is another method, this is called when, in this case, the 'subscribe' method of the class is called...

Don't worry if things aren't clear yet there not supposed to be :)

OK, so lets start making sense and breakdown the above running examples that will hopefully make all clear.


sub = Email_subscription('craig', 'craig.addyman@googlemail.com')

This is the creation of a new class object, so if we were to print this we would get the following...


print sub
# 

This is just the objects memory details. Now if we call 'name' off the newly created object we will get a the name that we passed into the object, like so...


print sub.name
# craig

If it's not clear by now what is happening, the self argument refers to the object name, in this case 'sub'.To help you out, here is the method rewritten in a way that makes it easier to understand what is happening.


	def __init__(sub, 'craig', 'craig.addyman@googlemail.com'):
		'''Returns the user *name*, *email* and subscription status set to subscribed'''
		sub.name = 'craig'
		sub.email = 'craig.addyman@googlemail.com'
		sub.status = 'Subscribed'

I'm short for time right now so just adding bits to this as and when I can. Sorry.


Give Your Inbox Some Love


What You'll get?
  • Posts like this delivered straight to your inbox!
  • Nothing else.
Comment Policy

Any code snippets more than a line or 2, please include as a link to a gist

comments powered by Disqus