2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MD5によるダイジェスト値を取得する

Last updated at Posted at 2021-11-25
$ cat test1.py
#!/usr/bin/env python3

import hashlib
import datetime


class Animal:
  def __init__(self, name='John', height=180):
    dt_now = datetime.datetime.now()
    self._id = hashlib.md5(str(dt_now).encode()).hexdigest()
    self._name = name
    self._height = height

  def __del__(self):
    print(self._id, 'is destruted...')

  @property
  def id(self):
    return self._id

  @property
  def name(self):
    return self._name

  @property
  def height(self):
    return self._height

  @name.setter
  def name(self, name):
    self._name = name

  @height.setter
  def height(self, height):
    self._height = height

  def get(self):
    return self._id, self._name, self._height


if __name__ == '__main__':
  # just for testing

  # dt_now = datetime.datetime.now()
  # print(dt_now)

  # hs = hashlib.md5(str(dt_now).encode()).hexdigest()
  # print(hs)

  a1 = Animal()
  print(a1.id)
  print(a1.name)
  print(a1.height)
  print(a1.get())

  a2 = Animal(name='Paul', height=183)
  print(a2.id)
  print(a2.name)
  print(a2.height)
  print(a2.get())

  a3 = Animal(name='George', height=105)
  print(a3.id)
  print(a3.name)
  print(a3.height)
  print(a3.get())

  del a1, a2, a3
% ./test1.py
d097b3d674dda8dec898a8e4def18ab1
John
180
26c1a6744fa3d9f7e63640a3a9a8f79a
Paul
183
('26c1a6744fa3d9f7e63640a3a9a8f79a', 'Paul', 183)
3caf70515aa5aa97c9ce6dffb58ce8f8
George
105
('3caf70515aa5aa97c9ce6dffb58ce8f8', 'George', 105)
d097b3d674dda8dec898a8e4def18ab1 is destruted...
26c1a6744fa3d9f7e63640a3a9a8f79a is destruted...
3caf70515aa5aa97c9ce6dffb58ce8f8 is destruted...
PS D:\MyWork\python-test\object-test> python .\test1.py
3580ad44cd6fcdf2b1576354954507df
John
180
('3580ad44cd6fcdf2b1576354954507df', 'John', 180)
c13b973b08263fbff9ddcaf64865277c
Paul
]]
183
('c13b973b08263fbff9ddcaf64865277c', 'Paul', 183)
c13b973b08263fbff9ddcaf64865277c
George
105
('c13b973b08263fbff9ddcaf64865277c', 'George', 105)
3580ad44cd6fcdf2b1576354954507df is destruted...
c13b973b08263fbff9ddcaf64865277c is destruted...
c13b973b08263fbff9ddcaf64865277c is destruted...
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?