Last active
February 28, 2019 08:51
-
-
Save bonjourmauko/255822ddcdd686d626e63c8266861af9 to your computer and use it in GitHub Desktop.
NamedTuple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from typing import NamedTuple | |
import numpy | |
import time | |
class Rectangle(NamedTuple): | |
height: float | |
width: float | |
def add_width(self, amount: int) -> Rectangle: | |
return self.__class__(self.height, self.width + amount) | |
my_rectangle1 = Rectangle(2.0, 2.0) | |
my_rectangle2 = my_rectangle1.add_width(-1.0) | |
print(my_rectangle1 is not my_rectangle2) | |
print([2.0, 2.0] == list(my_rectangle1)) | |
print([2.0, 1.0] == list(my_rectangle2)) | |
my_data1 = numpy.random.random_sample((100000, 2)) | |
my_data2 = numpy.random.random_sample((1000000, 2)) | |
my_data3 = numpy.random.random_sample((10000000, 2)) | |
start = time.time() | |
my_collection1 = [Rectangle(*attrs) for attrs in my_data1] | |
end = time.time() | |
print(end - start) | |
start = time.time() | |
my_collection2 = [Rectangle(*attrs) for attrs in my_data2] | |
end = time.time() | |
print(end - start) | |
start = time.time() | |
my_collection3 = [Rectangle(*attrs) for attrs in my_data3] | |
end = time.time() | |
print(end - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://realpython.com/python-data-classes/