5th unit--classes and obj Flashcards
py class with syn
*It is a user defined data structure that binds data members and methods into single unit
* Blueprint or object constructor for creating objects
* Class keyword is used to define it
Syn: class classname:
stmts
object-syntax
- We can use class name to create object
syntax: obj= classname()
__init()__ (3) eg
- built in function
- This **automatically executed an object of classes created **
- In Python every **object can have its own value for attributes of class **this can be achieved by in it method
- It is like a **constructor which allows class to hold objects with different values **
- We dont need to call the function like typical functions or methods … It is automatically executed when an object is created
- Eg: class classname:
def __init__(obj, props_ _ _0):
obj.prop= prop
|
|
self(4)
- Acts as this pointer in C++
- Self parameter is a reference to current instance of class
- Used to access variable that belongs to class
- We can name it anything we want but it should be only first place __init__(self, )
pass
Class definition cannot be empty, But for some reasons if we don’t have Anything to declare we can put “pass” To avoid errors
Data abstraction hiding through classes
- Data abstraction refers to the process by which data and functions are defined in such a way that only essential details are provided to the outside world and implementation details are hidden
- Classes Follow data abstraction i.e. Provide the functionality of object or manipulation of object data and hiding implementation details of class or method to the outside entity
-
Data encapsulation: Is also called as data hiding
–Wrapping up the data Into one unit
– This prevents data modification accidentally by limiting access only to the variables and methods
–organising the data and methods into structure that prevents data access by a functional method that is not specified in the class—- This ensures the integrity of data contained in the object - Data encapsulation provides different access levels for the variables and members of function of class
Access levels
Public:
1. accessed by any function belonging to any class
2. Lowest level of data protection
Private:
1. accessed only by the class in which it is declared
2. Highest level of data protection
3. Prefixed by double _ _ eg: ** _ _ person**
built-in cls attri
- __dict__ Gifts dictionary contains class or objects
- __doc__ Provides documentation of an object (o/p–none)
- __name__ Name of the class
- __module__ Name of the model in which class/ obj it is defined
- __bases__ Returns base class or base class lists
Class variable
- Class variables are declared inside the construction of class
- Since these variables are owned by class itself they are shared by class instances also
- These variables are defined inside a class just after declaration of class header before Declaring any method of constructor and other functions
eg:
#defining the class
class Class_name:
#declaring the variable in the class
var = “xyz”
#instantiating the class
myObj = Class_name()
Object/instance variable
instantiating the class
- Variables that are owned by a class objects are called as object variables
- Unlike class variables object variables are defined inside the function
eg:
#defining the class
class Student:
# using the initializing function
def __init__(self, id, name, age):
self.id = id
self.name = name
self.age = age
dBase = Student(102, “Sam”, 13)
#printing the required values
print(“Roll Number of the Student:”, dBase.id)
print(“Name of the Student:”, dBase.name)
print(“Age of the Student:”, dBase.age)
Del()
- Initiate a object is created and delete is automatically called when an object is going out of scope
- Object goes out of scope when object is no longer used and its resources are occupied returned back to the system so that others can reuse them
- DEL keyboard also explicitly do the same
programme isnt the class work
cls attri
1.