*Please Note* - all presentations and code challenges+solutions are available to subscribing members. Sometimes the best way to learn something is to solve problems and learn as you go along! Please watch the introductory video on OOP and Classes below before you get started on the challenges that will take you through from absolute basics to understanding how object orientated programming and classes work in Python.
If you haven't worked with classes before, you'll find that they open up a whole new world. Once you get the hang of them, you may never be able to return to the way you were programming before! Interestingly, the concept of classes has its roots in some very deep philosophy. The Greek philosopher Plato (427 BC) is considered to be one of the greatest philosophers of all time. Plato asserted that the objects we see in our physical world—trees, chairs, horses, plants and people—everything we see, touch, taste and smell is not actually real. Only the idea or true blueprint behind a thing is real. Or to put it another way, everything we see, is a sort of created copy (the programming word for this is instantiation) of the true thing. For example, there are many types of horses in the world, but we recognise them to be horses because there is one perfect horse (or the idea/blueprint of a horse) that exists in a 'heavenly' plane as a “form”. Because only ideas of things are perfect, Plato asserted that the realm of ideas was far superior to this physical world we live in. A similar thought is expressed by the Bible that suggests that there is a heavenly realm in which the blueprint or 'true reality' of things actually exists. The apostle Paul in Colossians 2:17 writes: "These are a shadow of the things that were to come; the reality, however, is found in Christ." As you learn about Classes, it is helpful (as a beginner) to think of them as a blueprint for the creation of an object. They are incredibly powerful and you will enjoy the flexibility and new understanding of programming they will bring!
In this section we will provide examples of the use of classes and how OOP works as well as challenges for you to try. We will also cover the four pillars of OOP - Abstraction, Inheritance, Encapsulation and Polymorphism. Big words, but once you have done them for yourself, you'll never forget what they mean.
""" Task: 1. Create another shape (square) with width and height 100 x 100 2. Add a description and an author for the new shape 3. Calculate the area for the new square defined above 4. Make the square 50% smaller 5. Find the perimeter of your square """ from turtle import* class Shape: #self is how we refer to things in the class from within itself. .self is the first parameter in any function defined inside a class #to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name) def __init__(self,x,y): self.x=x #the shape has the attribute x (width) self.y=y #the shape has the attribute y (height) description="The shape has not yet been brought into being" author="No one has yet claimed authorship of this shape" canvas = Screen() canvas.setup(800,800) turtle_obj = Turtle() def area(self): return self.x*self.y def perimeter(self): return 2*self.x+2*self.y def describe(self,text): self.description =text def authorName(self,text): self.author=text def scaleSize(self,scale): self.x=self.x*scale self.y=self.y*scale def printDimension(self): print("The dimensions of this shape are: width>>",self.x,"and height>>",self.y) def draw(self): self.turtle_obj.forward(self.x) self.turtle_obj.left(90) self.turtle_obj.forward(self.y) self.turtle_obj.left(90) self.turtle_obj.forward(self.x) self.turtle_obj.left(90) self.turtle_obj.forward(self.y) self.turtle_obj.left(90) #Specify width and height below e.g. 100,200 square=Shape(100,200) square.printDimension() square.draw()
The concept of OOP and Classes is incredibly interesting and if you are learning this you are probably at an advanced level of learning python (A level/College/University or beyond). Object Orientated Programming is an approach that will take some thinking and re-thinking, but it will be well worth it. We start, as do all the solve and learn series sessions, with an introductory power point. You can then go through and solve/analyse all the challenges. Each challenge introduces various OOP concepts, and the pillars of object orientated programming such as abstraction, encapsulation, inheritance and polymorphism will be looked at.
Download Python 3 here: