3.2 - 绑定方法与非绑定方法练习题

定义MySQL类
要求:
1.对象有id、host、port三个属性
2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一
3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化
 1 import hashlib
 2 import string
 3 import random
 4 import settings
 5 
 6 class MySql:
 7     def __init__(self,host,port):
 8         self.id=self.create_id()
 9         self.host=host
10         self.port=port
11 
12     @classmethod
13     def from_conf(cls):
14         obj=cls(
15             settings.host,
16             settings.post
17         )
18         return obj
19 
20     @staticmethod
21     def create_id():
22         m=hashlib.md5(''.join(random.sample(string.digits+string.ascii_letters+string.punctuation,8)).encode('utf-8'))
23         return m.hexdigest()
24 
25 
26 mysql=MySql('127.0.0.1',8086)
27 print(mysql.__dict__)
28 obj1=MySql.from_conf()
29 print(obj1.__dict__)
settings:文件
host='168.100.100.123'
post=8089

 

转载于:https://www.cnblogs.com/alice-bj/p/8552293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值