class Quadrangle:
width = 0
height = 0
color = "black"
square = Quadrangle()
class Quadrangle:
self.width = 0
self.height = 0
self.color = "black"
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-45-c2dd94dcbb71> in <module>() ----> 1 class Quadrangle: 2 self.width = 0 3 self.height = 0 4 self.color = "black" <ipython-input-45-c2dd94dcbb71> in Quadrangle() 1 class Quadrangle: ----> 2 self.width = 0 3 self.height = 0 4 self.color = "black" NameError: name 'self' is not defined
class Quadrangle:
def __init__(self, width, height, color):
self.width = width
self.height = height
self.color = color
square = Quadrangle()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-47-34362c3785a3> in <module>() ----> 1 square = Quadrangle() TypeError: __init__() missing 3 required positional arguments: 'width', 'height', and 'color'
square = Quadrangle(5, 5, "black")
square.width
5
class Quadrangle:
def __init__(self, width, height, color):
self.width = width
self.height = height
self.color = color
def __del__(self):
print("Quadrangle object is deleted")
square = Quadrangle(5, 5, "black")
Quadrangle object is deleted
del square
Quadrangle object is deleted
import math
class Figure:
def __init__(self, length, name):
self.length = length
self.name = name
def get_area(self):
return (math.sqrt(3) / 2) * self.length**2 # (math.sqrt(3) / 2) * math.pow(self.length, 2) 도 가능함
def get_name(self):
return self.name
def __del__(self):
print ('object is deleted')
square = Figure(10, 'dave')
print (square.get_area(), square.get_name())
object is deleted 86.60254037844386 dave
# 한발짝 더 나가보기!(심화 문제)
# 정삼각형 클래스를 만들고, 너비 출력하기
import math
class Quadrangle:
def __init__(self, length):
self.length = length
def get_area(self):
return (math.sqrt(3) / 2) * self.length**2 # (math.sqrt(3) / 2) * math.pow(self.length, 2) 도 가능함
square = Quadrangle(10)
square.get_area()
86.60254037844386