跳转到内容

Python 编程入门 / Python 编程 - 类和对象

来自维基教科书,开放世界中的开放书籍

6. 类和对象

[编辑 | 编辑源代码]

Python 从一开始就使用面向对象编程,因此创建类、使用类和对象非常容易。

6.1. 创建类

[编辑 | 编辑源代码]

class 语句创建一个新的类定义。用户提供的类名紧跟在关键字 class 后面,后面跟着一个冒号,如下所示

   class myclass:
   ‘The documentation of the class can be added here’
   Class objects
   Class methods
   class myclass:
       'This docstring provides for the details of the class defined'
       count=0
       def __init__(self,name,paycheck):
           self.name=name
           self.paycheck=paycheck
           myclass.count+=1
       def displayworkerCount(self):
           print "Total no of worker %d" %myclass.count
       
       def displayworkerName(self):
           print "Name of the user",self.name,"'s salary", self.paycheck
       
   >>> worker1=myclass("Nobody",15000)
   >>> worker2=myclass("SNGET", 10000)
   >>> worker3=myclass("Somebody", 20000)
   >>> worker4=myclass("Everybody",25000)
   >>> worker1.count
   4
   >>> worker1.displayworkerCount()
   Total no of worker 4
   >>> worker1.displayworkerName()
   Name of the user Nobody 's salary 15000
   >>> print myclass.__doc__
   This docstring provides for the details of the class defined
   >>>

最终用户可以使用 hasattr 对象检查定义的类中是否定义了特定的方法或对象或实例。

   >>> worker1
   <__main__.myclass instance at 0x017D5990>
   >>> worker1.name
   'Nobody'
   >>> worker1.paycheck
   15000
   >>> hasattr(worker1,'age')
   False
   >>> hasattr(worker1,'displayworkerCount')
   True

最终用户可以使用以下方法随时添加、修改、设置或删除类和对象的属性。

   >>> worker5=myclass("","")
   >>> worker5.name
   ''
   >>> worker5.paycheck
   ''
   >>> worker5.name="New Body"
   >>> worker5.paycheck=50000
   >>>
   >>> worker5.name
   'New Body' 
   >>> worker5.paycheck
   50000
   >>> worker1.paycheck
   15000
   >>> worker1.paycheck=55000
   >>> worker1.paycheck
   55000
   >>> setattr(worker1, "paycheck", 60000)
   >>> worker1.paycheck
   60000
   >>> >>>
   >>> delattr(worker1, "name")
   >>> worker1
   <__main__.myclass instance at 0x0222EA80>
   >>> worker1.name
   Traceback (most recent call last):
     File "<pyshell#372>", line 1, in <module>
       worker1.name
   AttributeError: myclass instance has no attribute 'name'

6.2. 类内置属性

[编辑 | 编辑源代码]

默认情况下,所有 Python 类都具有以下内置属性,可以使用点运算符访问,就像 __doc__ 属性提供类的文档一样。

  1. __dict__ : 类的命名空间的字典
  2. __doc__ : 类文档字符串或无,如果未定义
  3. __name__ : 类名
  4. __module__ : 定义类的模块名称。此属性在交互模式中为 "__main__"
  5. __bases__ : 一个可能为空的元组,包含基类,按照它们在基类列表中的出现顺序排列。

尝试上述方法针对上述定义的 myclass,我们得到以下结果。

   >>> print "The name of the class is :", myclass.__name__
   The name of the class is : myclass
   >>> print "The name of the module is :", myclass.__module__
   The name of the module is : __main__
   >>> print "The bases for the class myclass is :", myclass.__bases__
   The bases for the class myclass is : ()
   >>> print "The dictionary of namespace for myclass are :",myclass.__dict__
   The dictionary of namespace for myclass are : {'count': 4, '__module__': '__main__', 'displayworkerCount': <function
   displayworkerCount at 0x02244B70>, 'displayworkerName': <function displayworkerName at 0x02244BB0>, '__doc__': 'This
   docstring provides for the details of the class defined', '__init__': <function __init__ at 0x02244B30>}
   >>> print "The document string for myclass is: ",myclass.__doc__
   The document string for myclass is:  This docstring provides for the details of the class defined
   >>>
华夏公益教科书