Bernard Aybouts - Blog - Miltonmarketing.com

Approx. read time: 3.7 min.

Post: Learn about programming Classes and Objects in Python

Learn about programming Classes and Objects in Python: Objects are an encapsulation of variables and

functions into a single entity.

 

Objects get their variables and functions from classes. Classes are essentially a template to create your objects.

A very basic class would look something like this:

[python] class MyClass:
variable = "blah"

def function(self):
print("This is a message inside the class.")[/python]

We’ll explain why you have to include that “self” as a parameter a little bit later. First, to assign the above class(template) to an

object you would do the following:

[python]

class MyClass:
variable = "blah"

def function(self):
print("This is a message inside the class.")

myobjectx = MyClass()[/python]

Now the variable “myobjectx” holds an object of the classMyClass” that contains the variable and the function

defined within the class called “MyClass“.

Accessing Object Variables

To access the variable inside of the newly created objectmyobjectx” you would do the following:

[python] class MyClass:
variable = "blah"

def function(self):
print("This is a message inside the class.")

myobjectx = MyClass()

print(myobjectx.variable)[/python]

Trinket.io on-line Python compiler

So, for instance, the below would output the string “blah“:

[python] class MyClass:
variable = "blah"

def function(self):
print("This is a message inside the class.")

myobjectx = MyClass()

print(myobjectx.variable)[/python]

Trinket.io on-line Python compiler

You can create multiple different objects that are of the same class(have the same variables and functions defined).

However, each object contains independent copies of the variables defined in the class. For instance, if we were to

define another object with the “MyClassclass and then change the string in the variable above:

[python] class MyClass:
variable = "blah"

def function(self):
print("This is a message inside the class.")

myobjectx = MyClass()
myobjecty = MyClass()

myobjecty.variable = "yackity"

# Then print out both values
print(myobjectx.variable)
print(myobjecty.variable)[/python]

Trinket.io on-line Python compiler

Accessing Object Functions

To access a function inside of an object you use notation similar to accessing a variable:

[python] class MyClass:
variable = "blah"

def function(self):
print("This is a message inside the class.")

myobjectx = MyClass()

myobjectx.function()[/python]

Accessing Object Functions

The above would print out the message, “This is a message inside the class.

Exercise

We have a class defined for vehicles. Create two new vehicles called car1 and car2. Set car1 to be a red convertible

worth $60,000.00 with a name of Fer, and car2 to be a blue van named Jump worth $10,000.00.

[python] # this is the answer code.
# define the Vehicle class
class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str

# your code goes here
car1 = Vehicle()
car1.name = "Fer"
car1.color = "red"
car1.kind = "convertible"
car1.value = 60000.00

car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00

# test code
print(car1.description())
print(car2.description())[/python]

Trinket.io on-line Python compiler

Note: Running above unfinished-code will yield the following error:

Traceback (most recent call last):
File “script.py”, line 13, in
print(car1.description())
NameError: name ‘car1’ is not defined

OR

NameError: name ‘car1’ is not defined on line 13 in main.py

Learn about programming Classes and Objects in Python

Related Videos:

Miltonmarketing.com Related Videos.

Related Posts:

Adding Python Comments

Create a PayPal Donation Shortcode – WordPress

Using the class attribute

Using CSS classes

Python Online Compiler Code on the Go

Why Python is the cooler programming language

Methods of teaching programming

Python Online Compiler Code on the Go

The 14 most popular programming languages, according to a study of 100,000 developers

CSS HTML JAVASCRIPT Online Compiler Code on the go

Using CSS classes and the class attribute

Object-Oriented Programming (OOP)

Python Basic String Operations

Learn Python Lists

Python Serialization (JSON)

Learn List Comprehensions in Python Programming

Python Conditions

Python String Formatting

Python Conditions

Alice Teaches OOP (Glossary of useful terms)

Learn Code Introspection Python Programming

Learn Python Variables and Types

Python Basic Operators

Learn about Numpy Arrays in Python programming

Introduction to Python Programming Language

Learn Modules and Packages in Python programming

Learn Partial functions Python programming

The Longevity Blueprint: AI-Powered Health Optimization

Current step:1AI-Human Medical Analyzer: Smarter, Personalized Health
2AI-Human Medical Analyzer: Smarter, Personalized Health

> SYS.HEALTH: AI-Human Medical Analyzer_

// Revolutionize Your Diagnostics

Experience the perfect blend of cutting-edge AI precision and expert human care. Our revolutionary analyzer turns your raw health data into personalized, actionable insights tailored just for you.

> INITIALIZING_BIOMETRIC_SCAN...

[+] DATA_INPUT

Securely upload complex health parameters, including lab bloodwork and comprehensive medical history.

[+] PROCESSING

Advanced algorithmic parsing combined with human-level oversight ensures hyper-accurate data interpretation.

[+] OUTPUT_MATRIX

Receive smarter, faster, and truly personalized care strategies to take immediate charge of your health journey.

A name/nickname is required to continue.

> TRANSLATION_MATRIX_ACTIVE...
[ LANG_EN ]
Knowledge Heals, Prevention Protects
[ LANG_HI ]
ज्ञान ठीक करता है, रोकथाम सुरक्षा करती है
[ LANG_ZH ]
知识治愈,预防保护
[ LANG_JA ]
知識は癒し、予防は守る
[ LANG_HE ]
הידע מרפא, המניעה מגנה
[ LANG_AR ]
المعرفة تُشفي، والوقاية تحمي
[ LANG_FR ]
La connaissance guérit, la prévention protège

> SYS.AUTH: Data Processing Consent_

[ AWAITING_AUTHORIZATION ] By providing consent, you allow us to process your uploaded data through our proprietary AI-Human analysis system.

  • [+] SECURE_REVIEW: This ensures your information is carefully reviewed using advanced AI technology and certified professional oversight to deliver personalized health insights.
  • [+] PRIVACY_LOCK: Your privacy is our strict priority. Your data will only be used for this specific diagnostic purpose.

> SYS.UPLOAD: Share Medical Records [OPTIONAL]_

[ USER_CONTROL_ACTIVE ] Uploading your medical records during registration is entirely optional. You can choose to bypass this step and provide data later if it suits your timeline.

You dictate the data flow: share as much or as little as you’re comfortable with, and let us guide you toward better health.

[+] FORMAT_SUPPORT

We accept all file formats, including photos, PDFs, text documents, and raw official medical data.

[+] DATA_YIELD

Increased inputs correlate with higher precision. The more info you share, the better we tailor your personalized insights.

> NEXT_STEPS: Post-Registration Protocol_

Once your registration is complete, a human specialist from our team will personally reach out to you within 3-10 business days. We will discuss your health journey and map out exactly how we can support you.

About the Author: Bernard Aybout (Virii8)

Avatar Of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀