Python Scripting - Week 7 Flashcards
What is the abbreviations of OOP ?
Object Oriented Programming
What is not a object in python ?
Reserved keywords are not objects
Can we assighn objects to variables ?
Yes,
print is an function object , we can assign it to variable and use it as print function ?
What is class instance ?
x = MyClass()
object of class
How to Define a class in Python ?
class myclass:
x=1234
def f():
return ‘Hello World’
How to access class variable ?
class myclass:
x=1234
def f():
return ‘Hello World’
a=myclass()
print(f”Class Variable : {a.x}”)
How to access class method ?
class myclass:
x=1234
def f():
return ‘Hello World’
a=myclass()
print(f”Class Variable : {a.x}”)
What is the first argument of function in class ?
The first argument of a function in class is always self , self is an instance of class, it implicitly passed
Which class object is mutable by class instance ?
I you want to modify class variable by class instance , you have to use list.
class Car():
num=[]
def __init__(self):
pass
a=Car()
a.num.append(1)
Car.num.append(1)
Which function returns list of the attributes and methods of any object ?
dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object
Define a dunder method ?
class Book:
def __str__(self):
return f”Its a Book Method”
b=Book()
print(b)
How to print class instance ?
We have to use dunder method
def__str__(self):
return f”This string will be printed when class instance is printed “