What is object in python with example?

Python Classes and Objects

Python Classes/Objects

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class:

Example

Create a class named MyClass, with a property named x:

class MyClass:
  x = 5

Try it Yourself »

Create Object

Now we can use the class named MyClass to create objects:

Example

Create an object named p1, and print the value of x:

p1 = MyClass[]
print[p1.x]

Try it Yourself »

The __init__[] Function

The examples above are classes and objects in their simplest form, and are not really useful in real life applications.

To understand the meaning of classes we have to understand the built-in __init__[] function.

All classes have a function called __init__[], which is always executed when the class is being initiated.

Use the __init__[] function to assign values to object properties, or other operations that are necessary to do when the object is being created:

Example

Create a class named Person, use the __init__[] function to assign values for name and age:

class Person:
  def __init__[self, name, age]:
    self.name = name
    self.age = age

p1 = Person["John", 36]

print[p1.name]
print[p1.age]

Try it Yourself »

Note: The __init__[] function is called automatically every time the class is being used to create a new object.

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

The string representation of an object WITHOUT the __str__[] function:

class Person:
  def __init__[self, name, age]:
    self.name = name
    self.age = age

p1 = Person["John", 36]

print[p1]

Try it Yourself »

Example

The string representation of an object WITH the __str__[] function:

class Person:
  def __init__[self, name, age]:
    self.name = name
    self.age = age

  def __str__[self]:
    return f"{self.name}[{self.age}]"

p1 = Person["John", 36]

print[p1]

Try it Yourself »

Object Methods

Objects can also contain methods. Methods in objects are functions that belong to the object.

Let us create a method in the Person class:

Example

Insert a function that prints a greeting, and execute it on the p1 object:

class Person:
  def __init__[self, name, age]:
    self.name = name
    self.age = age

  def myfunc[self]:
    print["Hello my name is " + self.name]

p1 = Person["John", 36]
p1.myfunc[]

Try it Yourself »

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.

The self Parameter

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class:

Example

Use the words mysillyobject and abc instead of self:

class Person:
  def __init__[mysillyobject, name, age]:
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc[abc]:
    print["Hello my name is " + abc.name]

p1 = Person["John", 36]
p1.myfunc[]

Try it Yourself »

Modify Object Properties

You can modify properties on objects like this:

Delete Object Properties

You can delete properties on objects by using the del keyword:

Delete Objects

You can delete objects by using the del keyword:

The pass Statement

class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.

Test Yourself With Exercises


Python Objects and Classes

Python is an object-oriented programming language. Unlike procedure-oriented programming, where the main emphasis is on functions, object-oriented programming stresses on objects.

An object is simply a collection of data [variables] and methods [functions] that act on those data. Similarly, a class is a blueprint for that object.

We can think of a class as a sketch [prototype] of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.

As many houses can be made from a house's blueprint, we can create many objects from a class. An object is also called an instance of a class and the process of creating this object is called instantiation.

Defining a Class in Python

Like function definitions begin with the def keyword in Python, class definitions begin with a class keyword.

The first string inside the class is called docstring and has a brief description of the class. Although not mandatory, this is highly recommended.

Here is a simple class definition.

class MyNewClass:
    '''This is a docstring. I have created a new class'''
    pass

A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions.

There are also special attributes in it that begins with double underscores __. For example, __doc__ gives us the docstring of that class.

As soon as we define a class, a new class object is created with the same name. This class object allows us to access the different attributes as well as to instantiate new objects of that class.

class Person:
    "This is a person class"
    age = 10

    def greet[self]:
        print['Hello']


# Output: 10
print[Person.age]

# Output: 
print[Person.greet]

# Output: "This is a person class"
print[Person.__doc__]

Output

10

This is a person class

Creating an Object in Python

We saw that the class object could be used to access different attributes.

It can also be used to create new object instances [instantiation] of that class. The procedure to create an object is similar to a function call.

>>> harry = Person[]

This will create a new object instance named harry. We can access the attributes of objects using the object name prefix.

Attributes may be data or method. Methods of an object are corresponding functions of that class.

This means to say, since Person.greet is a function object [attribute of class], Person.greet will be a method object.

class Person:
    "This is a person class"
    age = 10

    def greet[self]:
        print['Hello']


# create a new object of Person class
harry = Person[]

# Output: 
print[Person.greet]

# Output: 
print[harry.greet]

# Calling object's greet[] method
# Output: Hello
harry.greet[]

Output


Hello

You may have noticed the self parameter in function definition inside the class but we called the method simply as harry.greet[] without any arguments. It still worked.

This is because, whenever an object calls its method, the object itself is passed as the first argument. So, harry.greet[] translates into Person.greet[harry].

In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method's object before the first argument.

For these reasons, the first argument of the function in class must be the object itself. This is conventionally called self. It can be named otherwise but we highly recommend to follow the convention.

Now you must be familiar with class object, instance object, function object, method object and their differences.

Constructors in Python

Class functions that begin with double underscore __ are called special functions as they have special meaning.

Of one particular interest is the __init__[] function. This special function gets called whenever a new object of that class is instantiated.

This type of function is also called constructors in Object Oriented Programming [OOP]. We normally use it to initialize all the variables.

class ComplexNumber:
    def __init__[self, r=0, i=0]:
        self.real = r
        self.imag = i

    def get_data[self]:
        print[f'{self.real}+{self.imag}j']


# Create a new ComplexNumber object
num1 = ComplexNumber[2, 3]

# Call get_data[] method
# Output: 2+3j
num1.get_data[]

# Create another ComplexNumber object
# and create a new attribute 'attr'
num2 = ComplexNumber[5]
num2.attr = 10

# Output: [5, 0, 10]
print[[num2.real, num2.imag, num2.attr]]

# but c1 object doesn't have attribute 'attr'
# AttributeError: 'ComplexNumber' object has no attribute 'attr'
print[num1.attr]

Output

2+3j
[5, 0, 10]
Traceback [most recent call last]:
  File "", line 27, in 
    print[num1.attr]
AttributeError: 'ComplexNumber' object has no attribute 'attr'

In the above example, we defined a new class to represent complex numbers. It has two functions, __init__[] to initialize the variables [defaults to zero] and get_data[] to display the number properly.

An interesting thing to note in the above step is that attributes of an object can be created on the fly. We created a new attribute attr for object num2 and read it as well. But this does not create that attribute for object num1.

Deleting Attributes and Objects

Any attribute of an object can be deleted anytime, using the del statement. Try the following on the Python shell to see the output.

>>> num1 = ComplexNumber[2,3]
>>> del num1.imag
>>> num1.get_data[]
Traceback [most recent call last]:
...
AttributeError: 'ComplexNumber' object has no attribute 'imag'

>>> del ComplexNumber.get_data
>>> num1.get_data[]
Traceback [most recent call last]:
...
AttributeError: 'ComplexNumber' object has no attribute 'get_data'

We can even delete the object itself, using the del statement.

>>> c1 = ComplexNumber[1,3]
>>> del c1
>>> c1
Traceback [most recent call last]:
...
NameError: name 'c1' is not defined

Actually, it is more complicated than that. When we do c1 = ComplexNumber[1,3], a new instance object is created in memory and the name c1 binds with it.

On the command del c1, this binding is removed and the name c1 is deleted from the corresponding namespace. The object however continues to exist in memory and if no other name is bound to it, it is later automatically destroyed.

This automatic destruction of unreferenced objects in Python is also called garbage collection.

Deleting objects in Python removes the name binding

What is object explain with example in Python?

An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. Python is object-oriented programming language that stresses on objects i.e. it mainly emphasizes functions.

What is an object explain with example?

Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

What's an object in Python?

Python Objects and Classes An object is simply a collection of data [variables] and methods [functions] that act on those data. Similarly, a class is a blueprint for that object. We can think of a class as a sketch [prototype] of a house. It contains all the details about the floors, doors, windows, etc.

What is object and instance in Python?

Everything in Python is an object such as integers, lists, dictionaries, functions and so on. Every object has a type and the object types are created using classes. Instance is an object that belongs to a class. For instance, list is a class in Python. When we create a list, we have an instance of the list class.

Chủ Đề