LESSON 3 Flashcards
__ is a user-defined blueprint or prototype from which objects are created. __ provide a means of bundling data and functionality together
class, Classes
Creating a new class creates a new type of ___, allowing new instances of that type to be made.
object
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
is like a blueprint for an object
Class
Some points on Python class:
● Classes are created by keyword class.
● Attributes are the variables that belong to a class.
● Attributes are always public and can be accessed using the dot (.) operator. Eg.: My class.Myattribute
An ___ is an instance of a Class
Object
A class is like a ___while an instance is a ___ of the class with actual values
blueprint, copy
It is represented by the methods of an object. It also reflects the response of an object to other objects.
Behavior:
● An object consists of:
State:
Behavior:
Identity:
It is represented by the attributes of an object. It also reflects the properties of an object
State:
DECLARING CLASS OBJECTS is also known as
instantiating a class
It gives a unique name to an object and enables one object to interact with other objects
Identity:
When an object of a class is created,
the class is said to be
instantiated
All the ___ share the attributes
and the behavior of the class. But the
values of those attributes, i.e. the state
are __for each object.
instances, unique
A __ __ may have any number of
instances.
single class
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.
Self Parameter
does not require for you to name it “self”. You can use any other name instead of it.
Self Parameter
is similar to constructors in C++ and Java. Constructors are used to initializing the object’s state.
_ init _() method
In Python, ___ are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically
destructors
is known as destructor, which are called when an object gets destroyed.
_ del _() method
It is called when all references to the object have been deleted i.e when an object is garbage collected.
_ del _() method
The destructor was called ____ or when all the references to object are deleted, i.e when the reference count becomes zero, not when object went out of scope.
after the program ended
Advantages of using destructors in Python
Automatic cleanup
Consistent behavior
Easy to use
Supports object-oriented programming
Helps with debugging
Destructors provide __ ___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.
Automatic cleanup
Destructors ensure that an object is properly cleaned up, regardless of how it is used or when it is destroyed. This helps to ensure ___ ____ and can help to prevent bugs and other issues.
Consistent behavior
Destructors are easy to implement in Python and can be defined using the __del__() method.
Easy to use
Destructors are an important feature of object-oriented programming and can be used to enforce encapsulation and other principles of object-oriented design.
Supports object-oriented programming
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
Helps with debugging
is used to define how a class object should be represented as a string.
_ str _() method
It is often used to give an object a human-readable textual representation, which is helpful for logging, debugging, or showing users object information.
_ str _() method
are for data, unique to each instance and class variables are for attributes and methods shared by all instances of the class.
Instance variables
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.
Instance variables
We can define instance variables within 2 ways
● Using a constructor
● Using Getters and Setters (normal way)