Python 如何扩展图像类?
我想在PIL中扩展“Image”类Python 如何扩展图像类?,python,inheritance,python-imaging-library,Python,Inheritance,Python Imaging Library,我想在PIL中扩展“Image”类 #module Image def open(file): ... class Image: def method1:... def method2:... #module myOriginal from Image import Image class ExtendedImage(Image): def method3:... #module test import myOriginal im = myOriginal.open(
#module Image
def open(file): ...
class Image:
def method1:...
def method2:...
#module myOriginal
from Image import Image
class ExtendedImage(Image):
def method3:...
#module test
import myOriginal
im = myOriginal.open("picture.jpg")
结果:Error.myOriginal没有属性“open”
如何在不重写open()方法的情况下扩展Image类?,PIL的作者:
Image类的设计目的不是
由应用程序代码子类化。如果
如果您想要自定义行为,请使用
委托包装器
myOriginal.py:
要委派各个方法,请执行以下操作:
class ExtendedImage(object):
def __init__(self,img):
self._img=img
def method1(self):
return self._img.method1() #<-- ExtendedImage delegates to self._img
def method3(self):
...
test.py:
import Image
import myOriginal
im = myOriginal.ExtendedImage(Image.open("picture.jpg"))
im.method3()
类Image.Image!中没有open()方法!?
import Image
import myOriginal
im = myOriginal.ExtendedImage(Image.open("picture.jpg"))
im.method3()