Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108410 > unrolled thread
| Started by | Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> |
|---|---|
| First post | 2016-05-09 05:20 +0100 |
| Last post | 2016-05-09 19:59 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
A problem with classes - derived type Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2016-05-09 05:20 +0100
Re: A problem with classes - derived type Yann Kaiser <kaiser.yann@gmail.com> - 2016-05-09 06:51 +0000
Re: A problem with classes - derived type Peter Otten <__peter__@web.de> - 2016-05-09 08:59 +0200
Re: A problem with classes - derived type Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2016-05-09 19:59 +0100
| From | Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> |
|---|---|
| Date | 2016-05-09 05:20 +0100 |
| Subject | A problem with classes - derived type |
| Message-ID | <ngp370$eu6$1@gioia.aioe.org> |
Hi! Suppose I have a class A whose implementation I don't know about. That class A has a method f that returns a A object. class A: ... def f(self, <...>): ... Now I want to write B derived from A with method f1. I want f1 to return a B object: class B(A): ... def f1(self, <...>): ... res=f(<...>) How do I return res as a B object? Thanks.
[toc] | [next] | [standalone]
| From | Yann Kaiser <kaiser.yann@gmail.com> |
|---|---|
| Date | 2016-05-09 06:51 +0000 |
| Message-ID | <mailman.542.1462776725.32212.python-list@python.org> |
| In reply to | #108410 |
If you can't change A to use something like "type(self)(...)" to create its
return value, you could use the dark side and swap res's __class__:
res.__class__ = B
Or:
res.__class__ = type(self)
Do note that B.__init__ will not be run when you do this, so it is up to
you to execute any additional initialization B might require.
Alternatively if it makes sense for you you can call B's methods on an A
object like so:
B.b_method(a_object, ...)
On Mon, 9 May 2016 at 06:26 Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
wrote:
> Hi!
>
> Suppose I have a class A whose implementation I don't know about.
> That class A has a method f that returns a A object.
>
> class A:
> ...
> def f(self, <...>):
> ...
>
> Now I want to write B derived from A with method f1. I want f1 to return
> a B object:
>
> class B(A):
> ...
> def f1(self, <...>):
> ...
> res=f(<...>)
>
> How do I return res as a B object?
>
> Thanks.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
Yann Kaiser
kaiser.yann@gmail.com
yann.kaiser@efrei.net
+33 6 51 64 01 89
https://github.com/epsy
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-05-09 08:59 +0200 |
| Message-ID | <mailman.543.1462777389.32212.python-list@python.org> |
| In reply to | #108410 |
Paulo da Silva wrote:
> Hi!
>
> Suppose I have a class A whose implementation I don't know about.
> That class A has a method f that returns a A object.
>
> class A:
> ...
> def f(self, <...>):
> ...
>
> Now I want to write B derived from A with method f1. I want f1 to return
> a B object:
>
> class B(A):
> ...
> def f1(self, <...>):
> ...
> res=f(<...>)
>
> How do I return res as a B object?
In the general case you need enough knowledge about A to create a B instance
from an A instance:
class B(A):
@classmethod
def from_A(cls, a):
b = cls(...) # or B(...)
return b
def f1(self, ...):
return self.from_A(self.f(...))
If the internal state doesn't change between A and B, and A is written in
Python changing the class of the A instance to B
class B(A):
def f1(...):
a = self.f(...)
a.__class__ = B
return a
may also work.
[toc] | [prev] | [next] | [standalone]
| From | Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> |
|---|---|
| Date | 2016-05-09 19:59 +0100 |
| Message-ID | <ngqmmu$129o$1@gioia.aioe.org> |
| In reply to | #108410 |
Às 05:20 de 09-05-2016, Paulo da Silva escreveu: Thank you Yann and Peter. I really didn't know anything about those "things". So far I have worked a lot with classes but they are written by me. Now I needed to derive pandas.Series (for example) and it has some methods that return pandas.Series objects. And I needed to return the derived object, not the pandas.Series one. The problem is now fixed. I'll find some time to read a little more about this to improve my pyhon knowledge. Thank you very much.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web