Skip to content

Instantly share code, notes, and snippets.

@okanyenigun
Last active March 6, 2022 13:40
Show Gist options
  • Save okanyenigun/339709ff942657c1d00bac5882da5c87 to your computer and use it in GitHub Desktop.
Save okanyenigun/339709ff942657c1d00bac5882da5c87 to your computer and use it in GitHub Desktop.
class-dataclass
class Device:
def __init__(self,model_name, manufacturer, price, model_year):
self.model_name = model_name
self.manufacturer = manufacturer
self.price = price
self.model_year = model_year
phone1 = Device("Note 9","Samsung",1000,2020)
phone2 = Device("Note 9","Samsung",1000,2020)
phone3 = Device("11","Iphone",1200,2020)
print(phone1) #<__main__.Device object at 0x7f97d1bb6ac0>
print(id(phone1)) #140290036085424
print(id(phone2)) #140290036085520
print(phone1 == phone2) #False
from dataclasses import dataclass, field
@dataclass(order=True, frozen=False)
class Device:
sort_index: int = field(init=False,repr=False)
model_name : str
manufacturer : str
price : int
model_year : int
inventory : bool = True #we can define default atts
def __post_init__(self):
"""
initializes after init
"""
self.sort_index = self.price
phone1 = Device("Note 9","Samsung",1000,2020)
phone2 = Device("Note 9","Samsung",1000,2020)
phone3 = Device("11","Iphone",1200,2020)
print(phone1) #Device(model_name='Note 9', manufacturer='Samsung', price=1000, model_year=2020)
print(id(phone1)) #140290036088736
print(id(phone2)) #140290036085424
print(phone1 == phone2) #True
print(phone3 > phone2) #True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment