翼度科技»论坛 编程开发 python 查看内容

Python类和对象的绑定方法及非绑定方法

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
类中定义的方法大致可以分为两类:绑定方法和非绑定方法。其中绑定方法又可以分为绑定到对象的方法和绑定到类的方法。
一、绑定方法

1 对象的绑定方法

在类中没有被任何装饰器修饰的方法就是 绑定到对象的方法,这类方法专门为对象定制。
  1. class Person:
  2.     country = "China"
  3.     def __init__(self, name, age):
  4.         self.name = name
  5.         self.age = age
  6.     def speak(self):
  7.         print(self.name + ', ' + str(self.age))
  8. p = Person('Kitty', 18)
  9. print(p.__dict__)
复制代码
  1. {'name': 'Kitty', 'age': 18}
  2. print(Person.__dict__['speak'])
  3. <function Person.speak at 0x10f0dd268>
复制代码
speak即为绑定到对象的方法,这个方法不在对象的名称空间中,而是在类的名称空间中。
通过对象调用绑定到对象的方法,会有一个自动传值的过程,即自动将当前对象传递给方法的第一个参数(self,一般都叫self,也可以写成别的名称);若是使用类调用,则第一个参数需要手动传值。
  1. p = Person('Kitty', 18)
  2. p.speak()  # 通过对象调用
  3. #输出
  4. Kitty, 18
  5. Person.speak(p)  # 通过类调用
  6. #输出
  7. Kitty, 18
复制代码
2 类的绑定方法

类中使用 @classmethod 修饰的方法就是绑定到类的方法。这类方法专门为类定制。通过类名调用绑定到类的方法时,会将类本身当做参数传给类方法的第一个参数。
  1. class Operate_database():
  2.     host = '192.168.0.5'
  3.     port = '3306'
  4.     user = 'abc'
  5.     password = '123456'
  6.     @classmethod
  7.     def connect(cls):  # 约定俗成第一个参数名为cls,也可以定义为其他参数名
  8.         print(cls)
  9.         print(cls.host + ':' + cls.port + ' ' + cls.user + '/' + cls.password)
  10. Operate_database.connect()
复制代码
输出
  1. <class '__main__.Operate_database'>
  2. 192.168.0.5:3306 abc/123456
复制代码
通过对象也可以调用,只是默认传递的第一个参数还是这个对象对应的类。
  1. Operate_database().connect()  # 输出结果一致#输出<class '__main__.Operate_database'>
  2. 192.168.0.5:3306 abc/123456
复制代码
二、非绑定方法

在类内部使用 @staticmethod 修饰的方法即为非绑定方法,这类方法和普通定义的函数没有区别,不与类或对象绑定,谁都可以调用,且没有自动传值的效果。
  1. import hashlib
  2. class Operate_database():
  3.     def __init__(self, host, port, user, password):
  4.         self.host = host
  5.         self.port = port
  6.         self.user = user
  7.         self.password = password
  8.     @staticmethod
  9.     def get_passwrod(salt, password):
  10.         m = hashlib.md5(salt.encode('utf-8'))  # 加盐处理
  11.         m.update(password.encode('utf-8'))
  12.         return m.hexdigest()
  13. hash_password = Operate_database.get_passwrod('lala', '123456')  # 通过类来调用
  14. print(hash_password)
  15. #输出
  16. f7a1cc409ed6f51058c2b4a94a7e1956
复制代码
  1. ```python
  2. p = Operate_database('192.168.0.5', '3306', 'abc', '123456')
  3. hash_password = p.get_passwrod(p.user, p.password)  # 也可以通过对象调用
  4. print(hash_password)
  5. #输出
  6. 0659c7992e268962384eb17fafe88364
复制代码
简而言之,非绑定方法就是将普通方法放到了类的内部。
三、练习

假设我们现在有一个需求,需要让Mysql实例化出的对象可以从文件settings.py中读取数据。
  1. # settings.py
  2. IP = '1.1.1.10'
  3. PORT = 3306
  4. NET = 27
复制代码
  1. # test.py
  2. import uuid
  3. class Mysql:
  4.     def __init__(self, ip, port, net):
  5.         self.uid = self.create_uid()
  6.         self.ip = ip
  7.         self.port = port
  8.         self.net = net
  9.     def tell_info(self):
  10.         """查看ip地址和端口号"""
  11.         print('%s:%s' % (self.ip, self.port))
  12.     @classmethod
  13.     def from_conf(cls):
  14.         return cls(IP, NET, PORT)
  15.     @staticmethod
  16.     def func(x, y):
  17.         print('不与任何人绑定')
  18.     @staticmethod
  19.     def create_uid():
  20.         """随机生成一个字符串"""
  21.         return uuid.uuid1()
  22. #学习中遇到问题没人解答?小编创建了一个Python学习交流群:489111204
  23. # 默认的实例化方式:类名()
  24. obj = Mysql('10.10.0.9', 3307, 27)
复制代码
  1. obj.tell_info()
  2. 10.10.0.9:3307
复制代码
1 绑定方法小结

如果函数体代码需要用外部传入的类,则应该将该函数定义成绑定给类的方法
如果函数体代码需要用外部传入的对象,则应该将该函数定义成绑定给对象的方法
  1. # 一种新的实例化方式:从配置文件中读取配置完成实例化
  2. obj1 = Mysql.from_conf()
  3. obj1.tell_info()
  4. #输出
  5. 1.1.1.10:27
  6. print(obj.tell_info)
  7. #输出
  8. <bound method Mysql.tell_info of <__main__.Mysql object at 0x10f469240>>
  9. print(obj.from_conf)
  10. #输出
  11. <bound method Mysql.from_conf of <class '__main__.Mysql'>>
复制代码
2 非绑定方法小结

如果函数体代码既不需要外部传入的类也不需要外部传入的对象,则应该将该函数定义成非绑定方法/普通函数
  1. obj.func(1, 2)
  2. #输出
  3. 不与任何人绑定
  4. Mysql.func(3, 4)
  5. #输出
  6. 不与任何人绑定
  7. print(obj.func)
  8. #输出
  9. <function Mysql.func at 0x10f10e620>
  10. print(Mysql.func)
  11. #输出
  12. <function Mysql.func at 0x10f10e620>
  13. print(obj.uid)
  14. #输出
  15. a78489ec-92a3-11e9-b4d7-acde48001122
复制代码
来源:https://www.cnblogs.com/python1111/p/17172386.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具