Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] A Simple Example of Python Objects and Classes [+Video]

#1
A Simple Example of Python Objects and Classes [+Video]

In Python, everything is an object. Even integers are objects — this is different from programming languages like Java where integers, floats, and Booleans are primitive data types. In this way, Python is built on a rigorously consistent object-oriented paradigm.

Considering that objects are everywhere in Python, object-oriented programming is not well studied by Python coders. A huge mistake! Fortunately, you can fix this by example about of Python objects and classes.

If you manage to read over the whole tutorial, you’ll get rewarded with a PDF cheat sheet about classes and objects. So, stay with me! ?‍♂️

Full Example Code


Here’s a simple example of object orientation in Python. We create two classes Muggle and Wizard, and create multiple instances such as Vernon and Petunia (Muggles), as well as Tom (Wizard). Go ahead and skim over the example, I’ll explain it to you in a moment:

class Muggle: def __init__(self, age, name, liking_person): self.age = age self.name = name self.likes = liking_person class Wizard: def __init__(self, age, name): self.age = age self.name = name self.mana = 100 def like_me(self, victim): if self.mana >= 100: victim.likes = self.name self.mana = self.mana - 100 Vernon = Muggle(52, "Vernon", None)
Petunia = Muggle(49, "Petunia", Vernon)
Vernon.likes = "Petunia"
print(Vernon.likes) Wiz = Wizard(42, "Tom")
Wiz.like_me(Petunia)
Wiz.like_me(Vernon)
print(Petunia.likes=="Tom" and Vernon.likes=="Tom")

As you go over this tutorial, you can watch me explain it in the following video.

[Video] Python Classes Objects Example from Harry Potter




Classes Example Explained


You may ask: what is a class anyway?

Classes are “blueprints” for creating objects. A class description tells you everything an object consists of and is able to do (data and functionality).

Today, we are seeing these two concepts in action (with the usual Harry Potter examples):

class Muggle: def __init__(self, age, name, liking_person): self.age = age self.name = name self.likes = liking_person Vernon = Muggle(52, "Vernon", None)
Petunia = Muggle(49, "Petunia", Vernon)

From this code snippet, you can learn several things:

  1. We create a new class “blueprint” for Muggles by using the keyword class. It is an abstract concept — and it shows how every Muggle in our microcosm looks like.
  2. We use the “constructor” method __init__(...) to initialize the class with data. In our case, every Muggle has attributes age, name, and likes (a person they like). The first value of any class method (including the constructor) is a reference to the object itself. Remember the class describes the functionality and properties of concrete objects. As soon as you call the constructor in your code, Python creates an empty object which you can access using the name self.

Note that although the first argument is self when defining a method, you don’t actually specify this argument when calling the method. Python internally does it for you. This is just a piece of syntax that you have to remember.

The constructor is a special method of the Muggle class: it allows you to initiate a new object by using the name of the class as a function call. For example, the calls Muggle(52, "Vernon", None) and Muggle(49, "Petunia", Vernon) creates two new class objects both defining the three variables as follows:

Example Python Classes (Harry Potter)

You can see that these objects follow the same blueprint but are different instances of the class Muggle. The same type of properties but different DNA.

From now on, these objects live in your computer’s memory until Python garbage collects them.

Can you see the tragic element of the story so far? Petunia likes Vernon but he likes nobody. However, as the famous German author “Janosh” said, “if you are loved, you cannot help but giving a little bit of love back”. Let’s make it a happy end, shall we?

Vernon.likes = "Petunia"
print(Vernon.likes)

Puzzle 1: What is the output of this code snippet?

Let’s move on. Harry Potter would be boring without wizards. So let’s define the Wizard class so that we can create some wizards in our small world.

class Wizard: def __init__(self, age, name): self.age = age self.name = name self.mana = 100 def like_me(self, victim): if self.mana >= 100: victim.likes = self.name self.mana = self.mana - 100 Wiz = Wizard(42, "Tom")

Every Wizard object has three variables: age, name, and mana level (how much magic power the wizard has left). On top of that, we add a method called like_me(...) that is a magic formula the wizard can apply to any victim. If the wizard has enough mana left, he can force the victim to like them.

Tom (aka. Lord Voldemort) is lonely and wants to be liked, so he tries to get Petunia and Vernon to like him:

Wiz.like_me(Petunia)
Wiz.like_me(Vernon) print(Petunia.likes=="Tom" and Vernon.likes=="Tom")

Puzzle: Is Tom successful in making both like him?

Make sure that you have completely internalized the syntax of how to create classes and objects in Python. The most common source of confusion is to forget the self argument when defining a method and that the constructor is defined and called differently: __init__(...) vs Wizzard(...).

Puzzle Solutions


Puzzle 1: "Petunia"

Puzzle 2: False

Cheat Sheet OOP


To help you dive deeper into the most important features of Python object orientation, feel free to download the following PDF:

Python OOP Cheat Sheet

Download All 8+ Python Cheat Sheets

Download only this cheat sheet as PDF

Instagram Python OOP Tutorial


For an ultra-short Python object orientation tutorial, check out the following Instagram post (swipe right):

<script async="" src="//www.instagram.com/embed.js"></script>

The post A Simple Example of Python Objects and Classes [+Video] first appeared on Finxter.



https://www.sickgaming.net/blog/2021/04/...ses-video/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Set Methods [Video Guide] xSicKxBot 0 22 05-02-2021, 10:14 PM
Last Post: xSicKxBot
  [Tut] Python iter() — A Simple Illustrated Guide with Video xSicKxBot 0 81 03-08-2021, 12:28 AM
Last Post: xSicKxBot
  [Tut] Python Dictionary Get Value – A Simple Illustrated Guide xSicKxBot 0 57 03-03-2021, 09:59 AM
Last Post: xSicKxBot
  [Tut] Python round() — A Simple Guide with Video xSicKxBot 0 88 02-13-2021, 12:41 PM
Last Post: xSicKxBot
  [Tut] Python complex() — A Useless Python Feature? xSicKxBot 0 99 12-19-2020, 02:05 AM
Last Post: xSicKxBot
  [Tut] Matplotlib Scatter Plot – Simple Illustrated Guide xSicKxBot 0 102 11-25-2020, 01:23 AM
Last Post: xSicKxBot
  [Tut] A Simple Recommendation System With Pandas xSicKxBot 0 105 10-31-2020, 06:58 AM
Last Post: xSicKxBot
  [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 149 10-09-2020, 06:13 PM
Last Post: xSicKxBot
  [Tut] Hello World! A Python One-Liner to Get Started with Python Quickly xSicKxBot 0 144 09-07-2020, 06:10 PM
Last Post: xSicKxBot
  [Tut] How to Print Without Newline in Python—A Simple Illustrated Guide xSicKxBot 0 142 08-25-2020, 07:12 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] How to Check Your TensorFlow Versi...
Last Post: xSicKxBot
Today 01:57 AM
» Replies: 0
» Views: 5
(Indie Deal) FREE Slash It, Resident Evi...
Last Post: xSicKxBot
Today 01:56 AM
» Replies: 0
» Views: 5
News - Katy Perry’s Pokémon Song ‘Electr...
Last Post: xSicKxBot
Today 01:56 AM
» Replies: 0
» Views: 5
News - Here’s A Look At The Rockin’ Open...
Last Post: xSicKxBot
Today 01:56 AM
» Replies: 0
» Views: 6
MiniGDX Kotlin Game Development Framewor...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 7
Mobile - Mad Skills Motocross 3’s traile...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
Fedora - PipeWire: the new audio and vid...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
News - Random: Check Out These “Mock-Ups...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
News - Video: Pokémon Fan Reimagines The...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
News - Where Is Xur Today? (May 14-18) -...
Last Post: xSicKxBot
Yesterday 05:35 PM
» Replies: 0
» Views: 79

[-]
Twitter

Copyright © SickGaming.net 2012-2020