python在运行时给class添加方法,pythonclass,下面的代码演示如何在运行
python在运行时给class添加方法,pythonclass,下面的代码演示如何在运行
下面的代码演示如何在运行时给类添加__str__方法。通过这个例子也可以了解__dict__和__class__的用法
import stringimport newdef __str__(self): classStr = '' for name, value in self.__class__.__dict__.items( ) + self.__dict__.items( ): classStr += string.ljust( name, 15 ) + '\t' + str( value ) + '\n' return classStrdef addStr(anInstance): anInstance.__str__ = new.instancemethod(__str__, anInstance, anInstance.__class__)# Test itclass TestClass: classSig = 'My Sig' def __init__(self, a = 1, b = 2, c = 3 ): self.a = a self.b = b self.c = ctest = TestClass()addStr( test )print test
相关内容
- 偷取Bing每天的桌面,并设置为自己的桌面,bing,Python语
- python sqlite3的常规使用,pythonsqlite3常规,#Filename: D
- python读取目录下文件并生成日志,,import os f
- 用PyQt实现透明桌面时钟小部件!,pyqt桌面时钟部件,#c
- 获取文件夹大小的python代码,获取python代码,import osfr
- 穷举法-SSH暴力破解工具,穷举-ssh暴力破解,#!/usr/bin/e
- python在windows下截屏,pythonwindows截屏,python在windo
- 控制台刷新进度,控制台进度,import sysim
- python使用xlrd和xlwt读写excel,xlrdxlwt,传说中python操作m
- python读取excel中的链接地址,pythonexcel,使用hyperlink_
评论关闭