Python编程进阶:类创建、GUI应用与事件循环
1. Python类的创建与应用
在Python编程中,我们已经从类的使用者转变为类的创建者。以下是一些关键知识点:
- 特殊方法的使用 :通过 __init__()
方法初始化新创建的实例,还可以实现其他特殊方法,让自定义数据类型(类)的行为与Python内置类相似。
- 方法类型 :可以创建普通方法和静态方法,同时学会存储和访问实例数据与静态数据。
下面是一个 OrderedDict
类的 setAt
方法示例:
def setAt(self, index, value):
"""Sets the index-th item's value to the given value
>>> d = OrderedDict(dict(s=1, a=2, n=3, i=4, t=5, y=6))
>>> d.getAt(5)
6
>>> d.setAt(5, 99)
>>> d.getAt(5)
99
>>> d.setAt(19, 42)
Traceback (most recent call last):
...
IndexError: list index out of range
"""
s