Derived class problem

Robert Kern kern at taliesen.caltech.edu
Sun May 26 22:59:40 EDT 2002


In article <Pine.GSO.4.05.10205271022440.10447-100000 at sparc41>,
	Geiger Ho <s997659 at ee.cuhk.edu.hk> writes:
> Hi all,
> 
>   Consider,
> 
> class A:
>   def method1(self):
>       ...
>       child = A()
>       ...
> 
> class B(A):
>   pass
> 
> 
>   When I call B.method1(), I will create an A child. But this is not I
> intend to do. I wanna create a B child. Can anyone tell me how I can
> modify? I don't want to override method1 as this is a pretty big code.

Try

class A:
    def method1(self):
        child = self.__class__()

class B(A):
    pass

-- 
Robert Kern
Ruddock House President
kern at caltech.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter



More information about the Python-list mailing list