Classes and Objects in Python (Notes)

Advertisements
Advertisements
Advertisements3
Advertisements4
Advertisements6
Advertisements66

Classes and Objects: –
1). 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.In order to Create a Class use the keyword class.
2). Creating an Object: – example:- Now we can use the class named MyClass to create objects.Create an object named p1, and print the value of x.
3). 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.
4). 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.
5). Object Methods: – Objects can also contain methods. Methods in objects are functions that belong to the object.
6). 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.
7). Modify Object Properties: – You can modify properties on objects.
8). Delete Object Properties: – You can delete properties on objects by using the del keyword.
9). Delete Objects: – You can delete objects by using the del keyword,
10). 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.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Loading...

Author: Lakshmi Prasanna Ponnala

Completed M.Tech in Digital Electronics and Communication Systems.

Leave a Reply

error: Content is protected !!

Discover more from ece4uplp

Subscribe now to keep reading and get access to the full archive.

Continue reading