【代码学习】PYTHON 面向对象

一、方法重新

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class Parent:        # 定义父类
   def myMethod(self):
      print '调用父类方法'
 
class Child(Parent): # 定义子类
   def myMethod(self):
      print '调用子类方法'
 
c = Child()          # 子类实例
c.myMethod()         # 子类调用重写方法

以上代码输出结果如下:

调用子类方法

二、循环导入模块

from functools import lru_cache

直接从functools模块中调用lru_cache

三、== 和 is

is   是比较两个引用是否指向了同一个对象(引用比较)

== 比较两个对象是否相等

四、__slots__()

__sloys__ 限制实例添加的属性

 

class Person(object):
    __slots__ = ("name","age")
P = Person()
P.name = "SQYY"
P.age = 19
P.score = 100 

 

运行结果

Traceback (most recent call last):
  File "C:\Python27\1.py", line 6, in <module>
    P.score = 100
AttributeError: 'Person' object has no attribute 'score'

 

posted @ 2017-07-15 11:04  水清云影  阅读(177)  评论(0编辑  收藏  举报