In this part of the course we'll be looking at object orientation (an object is a data structure that can hold different types of variables, when we move onto java or c# it'll make more sense but for now we're in python).
We'll first look at how subroutines/methods work.
Follow this example
def printThis(data):
print(data)
printThis("Hi")
printThis("I'm a function!")
This example doesn't particularly make sense because print is already a function however it's a good start as to what we will be getting onto. Alter around the strings that are printed or even what happens to the string (i.e. you can use [::-1] to reverse the string in the print statement on line 2).
Let's look at a program we've already covered, we'll take this one as an example
n = 1
while n <= 4:
print(n)
n += 1
Attempt to convert this into using a method (if you're stuck see below but it will be helpful to try it yourself).
def loop(n):
print(n)
return n+1
n = 1
if n <= 4:
loop(n)
You'll notice that while this makes the code more complex, it makes it easier for a programmer to read and maintain (especially if the code within the loop function is particularly long).
In the console we'll use the following code:
class User:
def __init__(firstName, lastName, userID, email):
self.__firstName = firstName
self.__lastName = lastName
self.userID = userID
self.__email = email
def getUserData(self):
return (self.__firstName, self.__lastName, self.userID, self.email)
I'll explain this bit before we continue:
Line one: we make a class (a series of data entries) and give it a name we can use to refer to it.
Line two: this is the class that will be called in order to generate a new user.
Line three until six (inclusive) are lines that define the variables passed to our function.
Line seven is a way of getting the data we have juse entered.
In line 8 we finally return this data in a friendly tuple (it's like a list but you can't edit it and it's dipslayed in python inside brackets ().
I did say I wasn't going to cover private, public and protected in this part of the cours but I feel like I have to do at least private and public:
Private - accessible to only that object (in this example user) - these veriables begin with a __
Public - accessible to anything universally within the program while that variable is being used (such as methods to get the information).
However, in python this doesn't actually happen as python likes to store variables a bit differently (and form its objects differently too).
Going back to the previous code we are now going to make our first object (hit enter until you get back the three ">" and you're ready).
Type
j = User("jack","g",50,"j@g.com")
satoshi = User("satoshi","nakamoto",2,"satoshi@satoshinakamoto.com")
Now run dir(j) and it will show you every command you can run on j (we're only interested in the ones that don't begin with an underscore _ as they're the ones we're legally aloowed to edit [under law of me]).
run print(j.userID())
and then print(satoshi.getUserData())
Two of the nicest structures to represent in classes are stacks and queues (these will be much slower in python but are much faster in c# and java if you only want limited functionality).
A stack is a lifo data structure. The
Last piece of data
In is the
First piece of data
Out.
We can do too main things to a stack:
POP this takes the last piece of data and outputs it
PUSH this adds an extra item to the stack.
(Disclaimer: I don't have python installed on my computer so this might be a nice debugging exercise for those of you who try this)
class node: # this will store each piece of data in the stack
def __init__(self, data):
self.__data = data
self.pointer = 0
def getData(self):
return self.__data
class stack: #creates our stack variable
def __init__(self):
self.__height = 0
self.__top = 0
def POP(self):
if self.__height > 0:
print(self.__top.getData())
self.__top = self.__top.pointer
else:
print("Stack underflow: nothing to remove")
def PUSH(self, data):
tmp = self.__top
self.__top = node(data)
self.__top.pointer = tmp
self.__height += 1
theStack = stack()
n = 0
while n != "-1":
action = input("Push or pop?")
if action.upper() == "PUSH":
pushWhat = input("what would you like to push?")
theStack.PUSH(pushWhat)
elif action.upper() == "POP":
theStack.POP()
n = input("would you like to continue, type -1 for no or anything otherwise")
I will leave this here as I feel that last bit of code will take a while to digest. Please pm me or bump the discussion thread if you don't understand anything,, principles of stacks are confusing (I'll check the code runs within about 10 minutes of posting it if you want to wait).
EDIT: broken code has been fixed!