0% found this document useful (0 votes)
15 views

OOP

The document defines several Python classes - Person, Student, Teacher and CyberStudent that inherit from each other. It then creates some instances of these classes and prints information about students whose grades meet or exceed a given threshold.

Uploaded by

noam99729
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

OOP

The document defines several Python classes - Person, Student, Teacher and CyberStudent that inherit from each other. It then creates some instances of these classes and prints information about students whose grades meet or exceed a given threshold.

Uploaded by

noam99729
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

class Person:

def __init__(self, name='Shooki', age=20):


self.__name = name
self.__age = age

def say(self):
return "Hi :)"

def __str__(self):
return return f"Person {self.__name} is {self.__age} years old"

def get_name(self):
return self.__name

def get_age(self):
return self.__age

def set_name(self, name):


self.__name = name

def set_age(self, age):


self.__age = age

class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.__grade = grade

def say(self):
return "Good Morning my friends from same class!"

def get_grade(self):
return self.__grade

def __str__(self):
return super().__str__() + f" and he is also a student with a grade
{self.__grade}"

def __repr__(self):
return "This is only for developer: " + super().__str__() + f" and
he is also a student in class {self.__grade}"

class Teacher(Person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self.__salary = salary

def say(self):
return "Good Morning cyber Students!"
def __str__(self):
return super().__str__() + f" and he is also a teacher with a salary
of {self.__salary}"

class CyberStudent(Student):
def __init__(self, name, age, grade, cyber_grade):
super().__init__(name, age, grade)
self.__cyber_grade = cyber_grade

def get_cyber_grade(self):
return max(self.__cyber_grade, super().get_grade())

def say(self):
return "Good Morning. We are the best!"

rami = CyberStudent("Rami", 16, 90, 95)


yael = CyberStudent("Yael", 17, 93, 85)
guy = Student("Guy", 15, 95)

students = [rami, yael, guy]

for student in students:


if student.get_cyber_grade() >= GOOD_GRADE:
print(student)
print("Wow!")

GOOD_GRADE = 94
for student in students:
print(student)
if isinstance(student, CyberStudent):
if student.get_cyber_grade() >= GOOD_GRADE:
print("Wow!")
else:
if student.get_grade() >= GOOD_GRADE:
print("Wow!")
Dunder
Usage / Needed for Learn more
method
__init__ Initialise object docs
__new__ Create object docs
__del__ Destroy object docs
__repr__ Compute “official” string representation / repr(obj) blog; docs
__str__ Pretty print object / str(obj) / print(obj) blog; docs
__bytes__ bytes(obj) docs
__format__ Custom string formatting blog; docs
__lt__ obj < ... docs
__le__ obj <= ... docs
__eq__ obj == ... docs
__ne__ obj != ... docs
__gt__ obj > ... docs
__ge__ obj >= ... docs
__hash__ hash(obj) / object as dictionary key docs
__bool__ bool(obj) / define Truthy/Falsy value of object blog; docs
__getattr__ Fallback for attribute access docs
__getattribute__ Implement attribute access: obj.name docs
__setattr__ Set attribute values: obj.name = value docs
__delattr__ Delete attribute: del obj.name docs
__dir__ dir(obj) docs
__get__ Attribute access in descriptor docs
__set__ Set attribute in descriptor docs
__delete__ Attribute deletion in descriptor docs
__init_subclass__ Initialise subclass docs
__set_name__ Owner class assignment callback docs
__instancecheck__ isinstance(obj, ...) docs
__subclasscheck__ issubclass(obj, ...) docs
__class_getitem__ Emulate generic types docs
__call__ Emulate callables / obj(*args, **kwargs) docs
__len__ len(obj) docs
__length_hint__ Estimate length for optimisation purposes docs
__getitem__ Access obj[key] blog; docs
__setitem__ obj[key] = ... or obj[] blog; docs
__delitem__ del obj[key] blog; docs
__missing__ Handle missing keys in dict subclasses docs
__iter__ iter(obj) / for ... in obj (iterating over) docs
__reversed__ reverse(obj) docs
__contains__ ... in obj (membership test) docs
__add__ obj + ... blog; docs
__radd__ ... + obj blog; docs
__iadd__ obj += ... blog; docs
__sub__ 2 3
obj - ... blog; docs
__mul__ 2 3
obj * ... blog; docs
__matmul__ 2 3
obj @ ... blog; docs
__truediv__ 2 3
obj / ... blog; docs
__floordiv__ 2 3
obj // ... blog; docs
Dunder
Usage / Needed for Learn more
method
__mod__ 2 3
obj % ... blog; docs
__divmod__ 2
divmod(obj, ...) blog; docs
__pow__ 2 3
obj ** ... blog; docs
__lshift__ 2 3
obj << ... blog; docs
__rshift__ 2 3
obj >> ... blog; docs
__and__ 2 3
obj & ... blog; docs
__xor__ 2 3
obj ^ ... blog; docs
__or__ 2 3
obj | ... blog; docs
__neg__ -obj(unary) blog; docs
__pos__ +obj (unary) blog; docs
__abs__ abs(obj) blog; docs
__invert__ ~obj (unary) blog; docs
__complex__ complex(obj) docs
__int__ int(obj) docs
__float__ float(obj) docs
__index__ Losslessly convert to integer docs
__round__ round(obj) docs
__trunc__ math.trunc(obj) docs
__floor__ math.floor(obj) docs
__ceil__ math.ceil(obj) docs
__enter__ with obj (enter context manager) docs
__exit__ with obj (exit context manager) docs
__await__ Implement awaitable objects docs
__aiter__ aiter(obj) docs
__anext__ anext(obj) docs
__aenter__ async with obj (enter async context manager) docs
__aexit__ async with obj (exit async context manager) docs

You might also like