1/18
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Class
is a user-defined blueprint or prototype from which objects are created. — provide a means of bundling data and functionality together.
Creating a new — creates a new type of object, allowing new instances of that type to be made.
Each — instance can have attributes attached to it for maintaining its state.
— instances can also have methods (defined by their class) for modifying their state.
creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.
Class
like a blueprint for an object
— are created by keyword —.
Attributes are the variables that belong to a —.
Attributes are always public and can be accessed using the dot (.) operator. Eg.: My —.Myattribute
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.
State
It is represented by the attributes of an object. It also reflects the properties of an object.
Behavior
It is represented by the methods of an object. It also reflects the response of an object to other objects.
Identity
It gives a unique name to an object and enables one object to interact with other objects.
Declaring Class Objects
also known as instantiating a class
When an object of a class is created, the class is said to be instantiated.
All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object.
A single class may have any number of instances.
Self Parameter
When we call a method of this object as pyobject.method(arg1, arg2), this is automatically converted by Python into PyClass.method(myobject, arg1, arg2) – this is all the special self is about.
The — does not require for you to name it “self”. You can use any other name instead of it.
Here we change the self to the word cats and the output will be the same.
Pass Parameter
The program’s execution is unaffected by the —statement’s inaction.
It merely permits the program to skip past that section of the code without doing anything.
It is frequently employed when the syntactic constraints of Python demand a valid statement but no useful code must be executed.
__init__() method
is similar to constructors in C++ and Java. Constructors are used to initializing the object’s state.
Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation.
It runs as soon as an object of a class is instantiated.
The method is useful to do any initialization you want to do with your object.
__del__() method
is known as destructor, which are called when an object gets destroyed.
In Python, destructors are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically.
It is called when all references to the object have been deleted i.e when an object is garbage collected.
The destructor was called after the program ended or when all the references to object are deleted, i.e when the reference count becomes zero, not when object went out of scope.
Automatic cleanup
Destructors provide automatic cleanup of resources used by an object when it is no longer needed. This can be especially useful in cases where resources are limited, or where failure to clean up can lead to memory leaks or other issues.
Consistent behavior
Destructors ensure that an object is properly cleaned up, regardless of how it is used or when it is destroyed. This helps to ensure consistent behavior and can help to prevent bugs and other issues.
Easy to use
Destructors are easy to implement in Python and can be defined using the __del__() method.
Supports object-oriented programming
Destructors are an important feature of object-oriented programming and can be used to enforce encapsulation and other principles of object-oriented design.
Helps with debugging
Destructors can be useful for debugging, as they can be used to trace the lifecycle of an object and determine when it is being destroyed.
_str_() method
method is used to define how a class object should be represented as a string.
It is often used to give an object a humanreadable textual representation, which is helpful for logging, debugging, or showing users object information.
When a class object is used to create a string using the built-in functions print() and —, the — function is automatically used.
You can alter how objects of a class are represented in string
Instance Variables
— are for data, unique to each instance and class variables are for attributes and methods shared by all instances of the class.
— are variables whose value is assigned inside a constructor or method with self whereas class variables are variables whose value is assigned in the class.
2 ways to define instance variables
Using a constructor
Using Getters and Setters (normal way)