almost done with OOP

This commit is contained in:
2025-03-09 17:48:54 +00:00
parent eef8158e96
commit a66bcc3af7

112
OOP/wiki.txt Normal file
View File

@@ -0,0 +1,112 @@
-> class in python:
for ex:
class PlayerCharacter:
#class object attributes
membership = True
def __init__(self, name, age):
if( PlayerCharacter.membership):
self.name = name #attributes
self.age = age
def run(self):
print('run')
return 'done'
player1 = PlayerCharacter('Cindy', 44)
player2 = PlayerCharacter('Tom', 21)
print(player1.name)
print(player2.name)
__init__: It's a dunder function.
It's like constructor of class.
It is defined for every class.
help(<object-name>): gives entire blueprint of the object
help(<class-name>): gives entire blueprint of the object
class object attributes: its like a static data member of class
-> @classmethod and @staticmethod:
for ex:
class dummy:
@classmethod
def fun1(cls, param1, param2):
...do something...
@staticmethod
def fun1( param1, param2):
...do something...
@classmethod and @staticmethod: Just like static member function
of class in C++ except in @classmethod
we use class attributes using 'cls'
keyword which acts like a this pointer
but in @staticmethod we can't do that.
-> private variable:
python doesn't provide any means to limit the access of data members
of class. The only thing smart is to use convention.
Any variable with underscore means it's a private variable and
don't modify it.
For ex:
_name is private variable in a class.
-> Inheritance:
For ex:
class User():
def sign_in(self):
print('logged in')
class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power
def attack(self):
print(f'attacking with power of {self.power}')
class Archer(User):
pass
wizard1 = Wizard()
print(wizard1.sign_in())
-> isinstance function:
isinstance is a function to check if the instance belongs
to a given class.
for ex:
isinstance(<instance>, <class>)
-> Object class in python:
Every class inherits from Object class.
-> Polymorphism:
The child class have function with same name doing different
functionality and the base class can also have same function
name but the child class could overide those function to do
its own functional logic
-> dir():
It specify what are the functions and variable the instance of
a class(object) has access to.
-> Dunder methods: