From a66bcc3af7447719313457ae9affa36c3124ec4a Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Sun, 9 Mar 2025 17:48:54 +0000 Subject: [PATCH] almost done with OOP --- OOP/wiki.txt | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 OOP/wiki.txt diff --git a/OOP/wiki.txt b/OOP/wiki.txt new file mode 100644 index 0000000..571098a --- /dev/null +++ b/OOP/wiki.txt @@ -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(): gives entire blueprint of the object + help(): 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(, ) + +-> 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: \ No newline at end of file