Recently I defined one of my Python classes as shown below.
from datetime import datetime, date, time
import enums
class ExampleClass:
defaults = (-1, "", "", datetime.today(), "", -1, [], "", -1, "", "", [], "")
def __init__(self, **kwargs):
count = 0
for ex in enums.ExampleEnums:
setattr(self, ex.name, kwargs.get(ex.value, ExampleClass.defaults[count]))
count += 1
def __str__(self):
return_string = "Example Object with "
count = 0
for val in enums.ExampleEnums:
if (getattr(self, val.name) != ExampleClass.defaults[count]):
return_string += str("%s: %s, " % (val.name, getattr(self, val.name)))
count += 1
return return_string[:-2]
def __repr__(self):
return_string = ""
count = 0
for val in enums.ExampleEnums:
if (getattr(self, val.name) != ExampleClass.defaults[count]):
return_string += str("%s=%s, " % (val.value, getattr(self, val.name)))
count += 1
return return_string[:-2]
def __eq__(self, other):
for val in enums.ExampleEnums:
if (getattr(self, val.name) != getattr(other, val.name)):
return False
return True
def __ne__(self, other):
for val in enums.ExampleEnums:
if (getattr(self, val.name) == getattr(other, val.name)):
return False
return True
Anyway, I'm wondering: is this a good way to write a class definition for a data class? Are there any ways I could improve this? I don't need any code, just generalities are fine as I'm only posting this as a way to see how I can improve my own coding abilities in Python.
Thanks