Skip to content

Instantly share code, notes, and snippets.

@dav1dddd
Created September 11, 2020 23:12
Show Gist options
  • Save dav1dddd/ee281f58e127fdc67e277165076aae7c to your computer and use it in GitHub Desktop.
Save dav1dddd/ee281f58e127fdc67e277165076aae7c to your computer and use it in GitHub Desktop.
class Code(object):
def __init__(self, name, age):
self.name = name
self.age = age
if not isinstance(self.name, str) and isinstance(self.age, int):
raise TypeError("self.name is not string or self.age is not int.")
def showArgs(self):
print(f"Name: {self.name}, Age: {self.age}")
def writeToFile(self):
# The with statement closes open automatically
with open("file.txt", "w") as f:
f.write(f"{self.name}\n")
class Hand(object):
def __init__(self, fingers):
self.fingers = fingers
if (self.fingers > 10):
raise ValueError("There are not more than 10 fingers (unless you are retarded?)")
# Instance method
def showFingers(self):
print(f"Fingers: {self.fingers}")
class Rectangle(object):
def __init__(self, length, width):
self.length = length
self.width = width
# Instance methods
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * self.length + 2 * self.width
# Class square inherits from square class
class Square(Rectangle):
def __init__(self, length):
# Inherits from Square class. "super()" gives you access to methods in a superclass from a subclass that inherits from it.
super().__init__(length, length)
obj = Square(4)
print(obj.area())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment