reading-notes

Code Fellows courses Notes

This project is maintained by QamarAlkhatib

Classes and Objects

Classes

Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Objects can contain arbitrary amounts and kinds of data. Classes partake of the dynamic nature of Python - they are created at runtime and can be modified further after creation.


Objects

A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods

Every object belonging to a class can have different properties. For example, Person(Human) can be treated as a class which has properties such as name, age,gender etc. Each individual will have different values of the properties of class Person. Everyone will also have different names, age and gender.

The Full code will be as


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

p1 = Person("John",20)

Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute −