
Overview
Since Python is an object-oriented, programming language, almost everything is an object with properties and methods. To create an object, a class is used to create a blueprint of the object.
Creating a Class
To create a class, the class keyword is used.
class my_class:
x = 2
print(my_class)
Output:
<class '__main__.my_class'>
Creating an Object
The class is then used to create an object.
class my_class:
x = 2
p1 = my_class()
print(p1.x)
Output:
2
The __init__() Function
All classes have a function called __init__(), which is always executed when the class is being initiated.
The __init__() function is used to assign values to object properties, or other operations that are necessary when the object is being created.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Johnny", 12)
print(p1.name)
print(p1.age)
Note
The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
It does not have to be named “self”, it can be called anything (“mySelf”, “my_parameter”, “cuteMonkey”), but it has to be the first parameter of any function in the class.
Output:
Johnny
12
The __str__() Function
The __str__() function controls what should be returned when the class object is represented as a string.
If the __str__() function is not set, the string representation of the object is returned.
Example 1: Without a string representation
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Johnny", 12)
print(p1)
Output:
<__main__.Person object at 0x14f197a1f100>
Example 2: With a string representation
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person("Johnny", 12)
print(p1)
Output:
Johnny(12)
Using Object Methods
Objects can also contain methods, which are functions that belong to the object.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("My son's name is " + self.name + ".")
p1 = Person("Johnny", 12)
p1.myfunc()
Output:
My son's name is Johnny.
Python Notes:
- The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
- Python string methods return new values, and DO NOT change the original string
- Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
- Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
- As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
- Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.