‘Python Classes/Objects’
A class in python is the blueprint from which specific objects arThe dir() function returns all properties and methods of the specified object, without the values.
This function will return all the properties and methods, even built-in properties which are default for all object.e created. It lets you structure your software in a particular way. Here comes a question how? Classes allow us to logically group our data and function in a way that it is easy to reuse and a way to build upon if need to be. Consider the below image
A Python class can be thought of as an abstract logical entity that serves as an initial prototype or template to construct objects. Python classes offer methods for changing an object’s state as well as specifying any qualities an object might possess.
x=’python’
print(x,type(x))
python <class 'str'>Python dir() Function
The dir() function returns all properties and methods of the specified object, without the values.
This function will return all the properties and methods, even built-in properties which are default for all object.
Syntax
dir(object)
Class: The class is a user-defined data structure that binds the data members and methods into a single unit. Class is a blueprint or code template for object creation. Using a class, you can create as many objects as you want
Objects have two characteristics: They have states and behaviors (object has attributes and methods attached to it) Attributes represent its state, and methods represent its behavior. Using its methods, we can modify its state.
In short, Every object has the following property.
- Identity: Every object must be uniquely identified.
- State: An object has an attribute that represents a state of an object, and it also reflects the property of an object.
- Behavior: An object has methods that represent its behavio
>>>A real-life example of class and objects.
Class: Person
- State: Name, Sex, Profession
- Behavior: Working, Study
Using the above class, we can create multiple objects that depict different states and behavior.
Get Alexekka’s stories in your inbox
Join Medium for free to get updates from this writer.
Object 1: Jessa
- State:
- Name: Jessa
- Sex: Female
- Profession: Software Engineer
- Behavior:
- Working: She is working as a software developer at ABC Company
- Study: She studies 2 hours a day
Object 2: Jon
- State:
- Name: Jon
- Sex: Male
- Profession: Doctor
- Behavior:
- Working: He is working as a doctor
- Study: He studies 5 hours a day
As you can see, Jessa is female, and she works as a Software engineer. On the other hand, Jon is a male, and he is a lawyer. Here, both objects are created from the same class, but they have different states and behaviors.
Object of a Class
An object is essential to work with the class attributes. The object is created using the class name. When we create an object of the class, it is called instantiation. The object is also called the instance of a class.
A constructor is a special method used to create and initialize an object of a class. This method is defined in the class.
In Python, Object creation is divided into two parts in Object Creation and Object initialization
- Internally, the
__new__is the method that creates the object - And, using the
__init__()method we can implement constructor to initialize the object.
Class Attributes
When we design a class, we use instance variables and class variables.
In Class, attributes can be defined into two parts:
- Instance variables: The instance variables are attributes attached to an instance of a class. We define instance variables in the constructor ( the
__init__()method of a class). - Class Variables: A class variable is a variable that is declared inside of class, but outside of any instance method or
__init__()method.







