Object Oriented Python



class Cat:
def __init__(self, color, legs):
self.color = color
self.legs = legs

felix = Cat("ginger", 4)
rover = Cat("dog-colored", 4)
stumpy = Cat("brown", 3)

> __init__ method 
This is called when an instance (object) of the class is created, using the class name as a function.
>self as their first parameter
> Python add self parameter
>self refers to the instance calling the method.

class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height

rect = Rectangle(7, 8)
print(rect.color)


> Inheritance

class Animal:
def __init__(self, name, color):
self.name = name
self.color = color

class Cat(Animal):
def purr(self):
print("Purr...")

class Dog(Animal):
def bark(self):
print("Woof!")

fido = Dog("Fido", "brown")
print(fido.color)
fido.bark()

A class that inherits from another class is called a subclass.
A class that is inherited from is called a superclass.
If a class inherits from another with the same attributes or methods, it overrides them.

Multiple Inheritance
class A:
  def method(self):
    print("A method")
    
class B(A):
  def another_method(self):
    print("B method")
super().method()
    
class C(B):
  def third_method(self):
    print("C method")
    
c = C()
c.method()
c.another_method()

c.third_method()

>The super function
 is a useful inheritance-related function 


Magic Methods

> special method
>have double underscores at the beginning and end of their names. 
>__init__
>__add__

__sub__ for -
__mul__ for *
__truediv__ for /
__floordiv__ for //
__mod__ for %
__pow__ for **
__and__ for &
__xor__ for ^
__or__ for |

__lt__ for <
__le__ for <=
__eq__ for ==
__ne__ for !=
__gt__ for >
__ge__ for >=

__len__ for len()
__getitem__ for indexing
__setitem__ for assigning to indexed values
__delitem__ for deleting indexed values
__iter__ for iteration over objects (e.g., in for loops)
__contains__ for in


class Vector2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector2D(self.x + other.x, self.y + other.y)


first = Vector2D(5, 7)
second = Vector2D(3, 9)
result = first + second
print(result.x)
print(result.y)

------------------

Object Lifecycle

creationmanipulation, and destruction.
When an object is destroyed, the memory allocated to it is freed up
>Weakly private methods and attributes have a single underscore at the beginning, is only a convention, and does not stop external code from accessing them.
from module_name import * won't import variables that start with a single underscore.

>Strongly private methods and attributes have a double underscore at the beginning,
can still be accessed externally, but by a different name


class Spam:
__egg = 7
def print_egg(self):
print(self.__egg)

s = Spam()
s.print_egg()
print(s._Spam__egg)
print(s.__egg)


__a of the class b be accessed from outside the class

> _b__a -------------

Thanks for sololearn.com
https://www.sololearn.com/Play/Python

Share this

Related Posts

Previous
Next Post »