import math as _math class Math(): def __init__(self, x, y): self.x = x self.y = y self.result = 0 def __str__(self): return str(self.result) class Pythagorus(Math): def calculate(self): self.result = _math.sqrt(self.x ** 2 + self.y ** 2) obj = Pythagorus(8,5) obj.calculate() print(obj) class Person(): def __init__(self, name, age): self.name = name self.age = age def __str__(self): return 'Hi! My name is {} and I am {} years old!'.format(self.name,self.age) class Student(Person): def __init__(self, name, age, loans): Person.__init__(self, name, age) self.loans = loans def __str__(self): return 'Hi! My name is {} and I am {} years old! But I owe ${} :/'.format(self.name, self.age, self.loans) person = Student('Samir',16,1002) print(person) class Person(): def __init__(self, name, age): self.name = name self.age = age def __str__(self): print('Person - ') return "My name is {}. I am {} years old. ".format(self.name, self.age) class Military(Person): def __init__(self, name, age, rank): Person.__init__(self, name, age) self.rank = rank def __str__(self): print('Military - ') return Person.__str__(self) + "I am a {}".format(self.rank) class Teacher(Person): def __init__(self, name, age, sub): print('Teacher - ') Person.__init__(self, name, age) self.sub = sub def __str__(self): return Person.__str__(self) + "I teach {}".format(self.sub) class Student(Person): def __init__(self, name, age, loans): Person.__init__(self, name, age) self.loans = loans class Animal: def __init__(self, name, bark): self.name = name self.bark = bark def Dog(self): print('{} is a dog, and dogs go {}!'.format(self.name, self.bark)) d1 = Animal('Rufus', 'Woof!') d1.Dog() class Person: population = 0 def __init__(self, name, age): self.name = name self.age = age print('{} has been born!'.format(self.name)) Person.population += 1 def __str__(self): return '{} is {} years old'.format(self.name, self.age) def __del__(self): print('{} is dying! :('.format(self.name)) Person.population -= 1 def totalPop(): print('There are {} people in the world.'.format(Person.population)) p1 = Person('Johnny', 20) print(Person.population) p2 = Person('Mary', 27) print(Person.population) print(p1) print(p2) class Student: def __init__(self, name): self.name = name self.attend = 0 self.grades = [] def addGrades(self, grade): self.grades.append(grade) def attendDay(self): self.attend += 1 def getAverage(): return sum(self.grades) / len(self.grades)
Python | Late to #CLASSES (geddit lel)
Finally, I have gotten the balls to start learning classes, haven't quite got the hang of them, but they are pretty interesting, so, look forward to great things, btw, I still have not given up my goal of creating a snake game before 2013 comes to it's end, I will, trust me, i will.