函数参数中的python UUID4

标签 python python-2.7

     def __init__(self, unique_id=uuid.uuid4())

如果用户未指定 ID,我希望实例化的每个对象都具有不同的 ID。当我实例化多个类时,它们都有相同的 UUID。我可以对这里发生的事情有一个技术概述,以便我可以更好地理解 Python 函数和初始化程序吗?

最佳答案

我相信它会让你的代码更干净,如果你只是将其添加到方法体中,即类似:

def __init__(self, unique_id=None):
    if not unique_id:
        unique_id = uuid.uuid4()

更紧凑的版本是:

def __init__(self, unique_id=None):
    unique_id = uuid.uuid4() if not unique_id else unique_id

如果需要,这将允许您覆盖 unique_id

关于函数参数中的python UUID4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45598138/

相关文章:

python - python 中的多重继承以及向继承类传递参数

python - 在 Python 中从字典中获取随机值

python - 查找后不读取所有字符

python - 如何定期使用opencv python更新图像的绘图窗口?

python - 如何使用 pycogent 在 python 2.7 中创建祖先序列?

python - 使用 Django、celery 和 Redis 安排任务

python - Berkeley DB是否只支持一种处理器操作

python - 如何删除列表中的所有奇数项?

python - Google Adwords API 身份验证问题

python - 使用 Tkinter 按 Enter 键时文本未保存在文本文件中