Have you wondered about Python Objects? In Python, an object is an instance of a class that has its own set of attributes and methods. In this article, we will explain how to create an object, and work with object attributes and methods with examples.
We will also introduce different methods to retrieve information about objects, such as the type(), dir(), id(), vars(), and help() functions.
Table of contents
1. What are Python Objects?
In Python object is an instance of a class, which is a blueprint for creating objects. Objects are created using the “class” keyword.
Each Object has attributes and methods. Attributes are variables that hold data. Methods are functions that operate on that data.
One of the key features of Python objects is that they are dynamic. This means that attributes and methods can be added or removed at runtime. This makes Python a highly flexible and powerful programming language for creating complex applications.
1.1 Example of Python Object
In Python, a string is an example of an object. We can create a string object by enclosing a sequence of characters in single or double quotes.
# Create String object
my_string = "I am String"
Strings in Python are objects, which means they have methods that can be called on them. For example, we can use the upper()
method to convert the string to all uppercase letters:
# Calling String method
string_upper = my_string.upper()
# "I AM STRING"
2. Create Python Object
We can create objects from built-in classes such as strings, integers, and lists, as well as from custom classes that we define ourselves. To create an object, we simply call the constructor of the class and pass any necessary arguments.
2.1 Built-in Objects
For example, to create a list object containing the numbers 1, 2, and 3, we can use the list() constructor like this:
# Create list object
my_list = list([1, 2, 3])
In the above example, my_list
is a list object that contains the numbers 1, 2, and 3. We can call certain methods available for this python object.
2.2 Custom Python Object
We can also create an object of our own custom class by defining the class and then calling its constructor. To create a custom object we first need to have a blueprint of that object. This blueprint of the object is called class.
# Create class
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
# Create a Car object
car1 = Car("Ford", "Mustang", 2022)
# Access the attributes of the Car object
print(car1.make) # Output: Ford
print(car1.model) # Output: Mustang
print(car1.year) # Output: 2022
3. Object Attributes
When we create an object in Python, we can assign attributes to it. An attribute is a variable that belongs to the object and can hold data. We can access these attributes using dot notation.
In the above example, we have a class called Car
with three attributes: make
, model
, and year
. These attributes are set in the __init__
method of the class, which is called when a new object of the class is created.
See how we can change the attributes of the Object:
# Change the make of the Car object
car1.make = "Chevrolet"
print(car1.make)
# Output:
# Chevrolet
4. Object Methods
Python objects can also have methods, which are functions associated with the object. These methods can be used to perform actions or operations on the object, or to return information about the object.
See the following Example:
# Create class with methods
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start_engine(self):
print("Starting the engine...")
# Create a Car object
car1 = Car("Ford", "Mustang", 2022)
# Call the start_engine method on the Car object
car1.start_engine()
# Output:
# Starting the engine...
In this example, the Car
class has a method start_engine
that prints a message to the console. When we create a Car
object and call the start_engine
method on it, the message is printed to the console.
5. Object Information Retrieval Methods
We’ll look at commonly used object information retrieval methods, including type(), dir(), id(), vars(), and help() functions.
The following methods provide insight into the structure and behavior of objects:
5.1 type() – Type of Object
The type() function is used to get the type of an object. It returns the class type of the argument passed as a parameter. The returned type is an object, which is the actual type of the object passed as a parameter.
# Get type of a object
print(type(car))
# Output:
# class '__main__.Car'
5.2 dir() – List Attributes and Methods
The dir()
function is used to get a list of all the attributes and methods of an object. When called without an argument, it returns a list of names in the current local scope.
# Get list of attributes & methods
print(dir(car1))
# Output:
# ['__class__', '__delattr__', 'make', 'model', 'start', 'year']
5.3 id() – Unique Identifier for an Object
The id()
function in Python returns a unique identifier for an object, which is an integer value that is guaranteed to be unique and constant for the life of the object.
print(id(car))
# Output:
# 140218279838512
5.4 help() – Get the Object docs
This function is a built-in function in Python that can be used to get help and documentation on a Python object. It can provide information on functions, modules, classes, and methods.
my_list = [1, 2, 3, 4, 5]
help(my_list)
it will display the documentation for the list
class in Python, including all of the methods and attributes that can be used with a list object.
6. Summary and Conclusion
You have learned about Python objects and how they are used in programming. Objects are instances of classes, and they represent a specific entity or concept. You have explored the fundamental features of objects, such as attributes and methods, and how they can be used. If you have any questions, don’t hesitate to ask in the comments section.
Happy learning!
Related Articles
- Print Object Properties and Values in Python?
- Explain Classes & Objects in Python
- Check object has an attribute in Python
- Python Functions Explained