python-2.7 - Python2.7:我如何为每个类对象制作可数 ID
问题描述
我想给我在 Employee 类中创建的每个员工一个 ID ,但自动
这是代码:
class employees :
def __init__(self,first,last,pay):
self.first_name = first
self.last_name = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
self.full_name = first + ' '+last
self.facebook_link = 'FB.com/'+ self.full_name
解决方案
class employees:
uid = 0
def __init__(self,first,last,pay):
self.first_name = first
self.last_name = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
self.full_name = first + ' '+last
self.facebook_link = 'FB.com/'+ self.full_name
employees.uid += 1
self.uid = employees.uid
现在,当我通过实例创建并打印它们时uid
emp1 = employees('Abhishek', 'Babuji', 1000)
print(emp1.uid)
输出:1
emp2 = employees('Abhishek1', 'Babuji1', 10001)
print(emp2.uid)
输出:2
每次你进去__init__
employees.uid
时都会增加 1,然后使用它分配给实例self.uid
推荐阅读
- python - 图片未上传。我想上传图片并将其保存到我提供媒体根目录和媒体网址的数据库中
- asp.net-core - IdentityServer4 从外部提供者创建访问令牌
- android-studio - 模拟器无法启动
- python - 从文本文件中读取点数据并返回字典
- wordpress - 请告诉wordpress phpmyadmin的config.inc.php文件在哪里
- python - 如何在python中将不同的图像保存到不同的目录中?
- javascript - JQuery onclick 数据属性“未定义”
- react-native - 如何更改 TabNavigator 中每个屏幕的标题?- 反应导航
- python - 使用 GCC for Python 构建 GPU 卸载 C++ 库的链接错误
- java - 串行通信 RXTX 和 Arduino 的问题