Skip to content

Instantly share code, notes, and snippets.

@psenger
Created November 20, 2023 10:07
Show Gist options
  • Select an option

  • Save psenger/505125efad5b889de6ea39cc018844a5 to your computer and use it in GitHub Desktop.

Select an option

Save psenger/505125efad5b889de6ea39cc018844a5 to your computer and use it in GitHub Desktop.
[Adding a toString to a class] #Python
def auto_str(cls):
def __str__(self):
return '%s(%s)' % (
type(self).__name__,
', '.join('%s=%s' % item for item in vars(self).items())
)
cls.__str__ = __str__
return cls
# @auto_str
# class Tag:
#
# def __init__(self, name, creator, organization, id=None):
# if id is None:
# self.id = uuid.uuid4()
# else:
# self.id = id
# self.name = name
# self.creator = creator
# self.organization = organization
# self.created_on = get_utc_datetime()
# self.modified_on = get_utc_datetime()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment