Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中扩展类?_Python_Class_Interface - Fatal编程技术网

如何在python中扩展类?

如何在python中扩展类?,python,class,interface,Python,Class,Interface,在python中,如何扩展类?例如,如果我有 color.py class Color: def __init__(self, color): self.color = color def getcolor(self): return self.color 颜色_.py import Color class Color: def getcolor(self): return self.color + " extended

在python中,如何扩展类?例如,如果我有

color.py

class Color:
    def __init__(self, color):
        self.color = color
    def getcolor(self):
        return self.color
颜色_.py

import Color

class Color:
    def getcolor(self):
        return self.color + " extended!"
但这不起作用。。。 我希望如果我在
color\u extended.py
中工作,那么当我创建一个color对象并使用
getcolor
函数时,它最终将返回字符串为“extended!”的对象。它还应该从导入中转到init

假设python 3.1

谢谢

使用:

import color

class Color(color.Color):
    ...
如果这是Python 2.x,您还需要从
对象
派生
color.color
,使其成为:

这在Python3.x中是不必要的。

另一种扩展(特别是指添加新方法,而不是更改现有方法)类的方法,即使是内置的,是使用预处理器,它可以扩展到Python本身的范围之外,在Python真正看到扩展之前,将其转换为普通Python语法

例如,我这样做是为了扩展Python2的
str()
str()
是一个特别有趣的目标,因为它隐式链接到引用的数据,例如
'this'
'that'

下面是一些扩展代码,其中唯一添加的非Python语法是
extend:testDottedQuad
位:

extend:testDottedQuad
def testDottedQuad(strObject):
    if not isinstance(strObject, basestring): return False
    listStrings = strObject.split('.')
    if len(listStrings) != 4: return False
    for strNum in listStrings:
        try:    val = int(strNum)
        except: return False
        if val < 0: return False
        if val > 255: return False
    return True
预处理器接受了这一点,在不使用monkeypatching的情况下吐出普通Python,Python完成了我希望它完成的任务

正如c预处理器向c添加功能一样,Python预处理器也可以向Python添加功能

我的预处理器实现对于堆栈溢出的回答来说太大了,但是对于那些可能感兴趣的人来说,它在GitHub上

class MyParent:

    def sayHi():
        print('Mamma says hi')

ChildClass
的实例将继承
sayHi()
方法。

您是否尝试阅读文档?类的首字母应该大写(“颜色”而不是“颜色”);)@wRAR也许在2013年,这是一个合理的问题,但老实说,人们首先会转向StackOverflow,所以这是一个很好的问题。这个问题是“python扩展类”在google上的第一个热门话题,文档是第三个。值得注意的是,您可以为新类赋予与旧类相同的名称:
class color(color):
定义一个新类,该类替换旧类,但它是从旧类派生的。(这似乎是OP试图做的。)
class extended\u颜色(color):
通常是不好的标准-
class ExtendedColor(color):
应该用于类。这里有一个吹毛求疵的问题:为什么不使用
\uuuu init\uuuu
?@Mentalist-由于该方法基于
Color
模块中现有的
Color
类,因此您只需要实现重载定义。例如,如果您没有在新类中实现
\uuuu init\uuu()
方法,但您所基于的类中有一个方法,那么它将只使用基类的
\uuu init\uuu()
方法。
if '192.168.1.100'.testDottedQuad():
    doSomething()

dq = '216.126.621.5'
if not dq.testDottedQuad():
    throwWarning();

dqt = ''.join(['127','.','0','.','0','.','1']).testDottedQuad()
if dqt:
    print 'well, that was fun'
class MyParent:

    def sayHi():
        print('Mamma says hi')
from path.to.MyParent import MyParent

class ChildClass(MyParent):
    pass