newbie: copy base class fields

tmp123 tmp123 at menta.net
Thu May 3 10:42:17 EDT 2007


On May 3, 4:29 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>
> > #!/usr/bin/python
> > #
>
> > class A:
> >   def __init__(self):
> >     self.v1=1
>
> >   def __repr__(self):
> >      return "v1=%d\n" % self.v1
>
> > class B(A):
> >   def __init__(self,a):
> >     self=a
> >     self.v2=2
>
> >   def __repr__(self):
> >      return A.__repr__(self) + ("v2=%d\n" % self.v2)
>
> > x=A()
> > print x
>
> > y=B(x)
> > print y
>
> > $ ./prueba.pl
> > v1=1
>
> > Traceback (most recent call last):
> >   File "./prueba.pl", line 23, in <module>
> >     print y
> >   File "./prueba.pl", line 17, in __repr__
> >     return A.__repr__(self) + ("v2=%d\n" % self.v2)
> >   File "./prueba.pl", line 9, in __repr__
> >     return "v1=%d\n" % self.v1
> > AttributeError: B instance has no attribute 'v1'
>


Hello Marc,

Thanks for your help.

I'm sorry, I've not correctly explained the subject. It is need to
init class B with the current value of the A instance, not with the
initial ones. A best example is:

x=A()
print x

x.v1=3

y=B(x)
print y

at the end, y.v1 must be equal to 3.

Sorry again.





More information about the Python-list mailing list