Lekcja – Wprowadzenie do klas w Pythonie
class Rectangle:
def __init__(self, lenght, height):
self.length = lenght
self.height = height
def field(self):
return self.length * self.height
def __repr__(self):
return f"<Rectangle L:{self.length} H:{self.height}>"
r1 = Rectangle(5, 4)
print(f"r1 - length {r1.length}, height {r1.height}, field {r1.field()}")
r2 = {
'length': 5,
'height': 4
}
def field_of_rectangle(r):
return r['length'] * r['height']
print(f"r2 - length {r2['length']}, height {r2['height']}, field {field_of_rectangle(r2)}")
print(r1)
print(r2)
Lab
import webbrowser
class Link:
def __init__(self, name, address, topic):
self.name = name
self.address = address
self.topic = topic
def open_it(self):
return webbrowser.open(self.address)
def __repr__(self):
return f"<{self.topic}: {self.name}>"
link_list = [
Link('Graphs', 'https://en.wikipedia.org/wiki/Graph_theory', 'Math'),
Link('Graph coloring', 'https://en.wikipedia.org/wiki/Graph_coloring', 'Math'),
Link('The best search engine', 'https://www.bing.com','Fun')
]
for link in link_list:
print(link)
link_list[-1].open_it()