Python - classless hooligan?
Tags: oop, python
Published: 2023-04-01 04:03:11

-ben tryon

Python3 has been used to create maps of both the modern and ancient world navigable by sound and touch. It has been used to create a weather forecast system that scores historical forecast data with actual weather data, producing data mining of striking accuracy. And it has been used for collaborative drug discovery. One could say Python is an amazing tool of the future, even the language of the future. But that only matters if the code works. Understanding the top-level properties within python is a powerful manner of ensuring working code.
            
Classes are user-defined. They are blueprints waiting to be filled with data and functionality, making them and python easy to reuse and maintain.  
For example, we define a Person class as follows:

```
class Person:
            def __init__(self, name, age):
                        self.name = name
                        self.age = age
```

*__init__ is a specific method that is called when an instance of class is created.
To create an object, we first define the class, then we create an instance of the class.  We would create an instance of the Person class like so: 
 
`person1 = Person(‘Awad’,30)`
 
In this example, we created an instance of the Person class with the name “Awad” and an age of 30 years old.
 
Instances are objects that are created from a class. They are unique, as each may have it’s own attributes.  Attributes are variables that belong to an object and are tiny containers, used to store data. In Python3, attributes can be defined at both the instance and the class levels. A gender attribute is added to the Person class, below:

```
class Person:
            def __init__(self, name, age, gender):
                        self.name = name
                        self.age = age
                        self.gender = gender
```
We then can update the instance of the updated class:
 
`person2 = Person(“Jewel”, 25, “female”) `
 
Class objects are objects that represent a class and are used to access class-level attributes and methods. Class-level attributes are shared by all instances of a class, while instance-level attributes are unique to each instance. Our Person class receives a class-level attribute, representing the total number of persons. It is then incremented:
 

```
class Person:
            def __init__(self, name, age, gender, count):
                        self.name = name
                        self.age = age
                        self.gender = gender
                        Person.count += 1
```
 
The count attribute can be accessed using the class object:
 

```
Print(Person.count)`
 
Method objects are objects that represent a method of a class. They can be used to access or call the method of an instance or of a class. Methods can be defined at both the instance and the class levels. 
 
In Python3, class-level methods are defined using the @classmethod decorator. A class-level method is added to our Person class to print the total number of persons:
 
class Person:
            count=0
            def __init__(self, name, age, gender, count):
                        self.name = name
                        self.age = age
                        self.gender = gender
                        Person.count += 1
            
            @classmethod
            def print_total_count(cls):
                        print(f”Total number of persons: {cls.count}”)
```
 
Understanding the top-level properties of Class and Method with their attendant attributes and instances will help any programmer utilize the powerful Python language.