Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The earth is like a tiny grain of sand, only much, much heavier.


devel / comp.lang.python / Re: How to "cast" an object to a derived class?

SubjectAuthor
* How to "cast" an object to a derived class?Robert Latest
`* Re: How to "cast" an object to a derived class?Stefan Ram
 `* Re: How to "cast" an object to a derived class?Robert Latest
  `- Re: How to "cast" an object to a derived class?Dieter Maurer

1
How to "cast" an object to a derived class?

<iqkbbeFq9l8U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=15209&group=comp.lang.python#15209

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblat...@yahoo.com (Robert Latest)
Newsgroups: comp.lang.python
Subject: How to "cast" an object to a derived class?
Date: 17 Sep 2021 20:15:42 GMT
Lines: 27
Message-ID: <iqkbbeFq9l8U1@mid.individual.net>
X-Trace: individual.net OR+/j+wZjbUHNwgspXEi5wQkZ/FjxIXzg27J/jlfGDK7ZJX5eM
Cancel-Lock: sha1:ozYO5O4vgRGatOFjGvUXJRDFx0k=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Fri, 17 Sep 2021 20:15 UTC

Hi all, let's assume I'm using a module that defines some class "Opaque" and
also a function that creates objects of that type. In my program I subclass
that type because I want some extra functionality. But how can I "promote" a
given Opaque instance to the derived class? Of course I could just create an
instance of the derived type and copy all attributes from the original object
to the new one, but that either breaks when the opaque.py module changes, or it
requires introspection. It's easily done of course but seems overly clumsy. Is
there a more Pythonic way?

# this is the module opaque.py
class Opaque(): def __init__(self, x): assert isinstance(x, int) self.n = x

def make_opaque(): return Opaque(0)

# this is my program
import opaque class MyClass(opaque.Opaque):
# generic __init__ b/c I don't want to have to know anything about that
# class
def __init__(self, *a, **k): super().__init__(*a, *k) self.__something = 0

def get_something(self): return self.something

op = opaque.make_opaque()

# But I want to have this as type MyClass. This obviously doesn't work:
my_object = MyClass(op)
# But what does?

Re: How to "cast" an object to a derived class?

<containment-20210917233847@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=15212&group=comp.lang.python#15212

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: How to "cast" an object to a derived class?
Date: 17 Sep 2021 22:41:33 GMT
Organization: Stefan Ram
Lines: 39
Expires: 1 Dec 2021 11:59:58 GMT
Message-ID: <containment-20210917233847@ram.dialup.fu-berlin.de>
References: <iqkbbeFq9l8U1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de /EO6uE8remwkUBWIJZrLcQh41F5J/cVZkZUXpvKLPCpiF8
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 17 Sep 2021 22:41 UTC

Robert Latest <boblatest@yahoo.com> writes: But how can I "promote" a
>given Opaque instance to the derived class?

Sometimes, one can use containment instead of inheritance.

class dot_class:
def __init__( self ):
self.x = 0
self.y = 0
def __str__( self ):
return f"dot( {self.x}, {self.y} )"

class colored_dot_class:
def __init__( self, dot = None ):
self.dot = dot if dot else dot()
self.r = 0
self.g = 0
self.b = 0
def __str__( self ):
return \
f"colored_dot( {self.r}, {self.g}, {self.b}, {self.dot}))"

dot = dot_class()
print( dot )
print( type( dot ))

# now "promote" dot to colored_dot
colored_dot = colored_dot_class( dot )
print( colored_dot )
print( type( colored_dot ))

output = r'''
dot( 0, 0 )
<class '__main__.dot_class'>
colored_dot( 0, 0, 0, dot( 0, 0 )))
<class '__main__.colored_dot_class'>
'''

Re: How to "cast" an object to a derived class?

<iqm6dqF679sU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=15218&group=comp.lang.python#15218

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblat...@yahoo.com (Robert Latest)
Newsgroups: comp.lang.python
Subject: Re: How to "cast" an object to a derived class?
Date: 18 Sep 2021 13:03:54 GMT
Lines: 12
Message-ID: <iqm6dqF679sU1@mid.individual.net>
References: <iqkbbeFq9l8U1@mid.individual.net>
<containment-20210917233847@ram.dialup.fu-berlin.de>
X-Trace: individual.net XDpmxYwbhYw5A9d14IXKIgGw6XwXYIQZmuCKhDO0tLJibiZAcg
Cancel-Lock: sha1:o67sLc2ybiwqR+UZAVVwDmo4CNw=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Sat, 18 Sep 2021 13:03 UTC

Stefan Ram wrote:
> Robert Latest <boblatest@yahoo.com> writes: But how can I "promote" a
>>given Opaque instance to the derived class?
>
> Sometimes, one can use containment instead of inheritance.

Nah, doesn't work in my case. I'm trying to write a wrapper around
xml.etree.ElemenTree and .Element to circumvent its idiotic namespace
handling. For that I need inheritance since I want to override the find..()
functions to return my derived MyElement classes. I think it could work but I
somehow need to convert the root Element to a MyElement.

Re: How to "cast" an object to a derived class?

<mailman.746.1632070193.4164.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=15228&group=comp.lang.python#15228

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: die...@handshake.de (Dieter Maurer)
Newsgroups: comp.lang.python
Subject: Re: How to "cast" an object to a derived class?
Date: Sun, 19 Sep 2021 18:22:55 +0200
Lines: 21
Message-ID: <mailman.746.1632070193.4164.python-list@python.org>
References: <iqkbbeFq9l8U1@mid.individual.net>
<containment-20210917233847@ram.dialup.fu-berlin.de>
<iqm6dqF679sU1@mid.individual.net>
<24903.25567.608277.813590@ixdm.fritz.box>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de Z3p9mAFjRlP9QDagRXnO/QPx8Se0QExIYP1eIe/Rdcvg==
Return-Path: <dieter@handshake.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'class,': 0.05; 'ram':
0.07; 'cc:addr:python-list': 0.09; 'derived': 0.09; 'instances':
0.09; 'namespace': 0.09; 'sometimes,': 0.09; 'subject:class':
0.09; 'writes:': 0.09; 'cc:no real name:2**0': 0.14; 'circumvent':
0.16; 'dieter': 0.16; 'instance': 0.16; 'risk.': 0.16; 'skip:>
20': 0.16; 'wrapper': 0.16; 'wrote:': 0.16; 'python': 0.16;
'instead': 0.17; 'cc:addr:python.org': 0.20; 'subject:How': 0.23;
'received:de': 0.23; 'cc:2**0': 0.25; 'robert': 0.26; 'think':
0.32; "doesn't": 0.32; 'but': 0.32; "i'm": 0.33; 'header:In-Reply-
To:1': 0.34; 'trying': 0.35; 'class': 0.37; 'could': 0.38; 'use':
0.39; 'wrote': 0.39; 'case.': 0.40; 'want': 0.40; 'try': 0.40;
'to:addr:yahoo.com': 0.64; 'your': 0.64; 'header:Received:6':
0.67; 'attribute': 0.84; 'inheritance': 0.84; 'received:88': 0.84
In-Reply-To: <iqm6dqF679sU1@mid.individual.net>
X-Mailer: VM 8.0.12-devo-585 under 21.4 (patch 24) "Standard C" XEmacs Lucid
(x86_64-linux-gnu)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <24903.25567.608277.813590@ixdm.fritz.box>
X-Mailman-Original-References: <iqkbbeFq9l8U1@mid.individual.net>
<containment-20210917233847@ram.dialup.fu-berlin.de>
<iqm6dqF679sU1@mid.individual.net>
 by: Dieter Maurer - Sun, 19 Sep 2021 16:22 UTC

Robert Latest wrote at 2021-9-18 13:03 GMT:
>Stefan Ram wrote:
>> Robert Latest <boblatest@yahoo.com> writes: But how can I "promote" a
>>>given Opaque instance to the derived class?
>>
>> Sometimes, one can use containment instead of inheritance.
>
>Nah, doesn't work in my case. I'm trying to write a wrapper around
>xml.etree.ElemenTree and .Element to circumvent its idiotic namespace
>handling. For that I need inheritance since I want to override the find..()
>functions to return my derived MyElement classes. I think it could work but I
>somehow need to convert the root Element to a MyElement.

If the class is a pure Python class, then the `__class__` attribute
of its instances is writable. You could try to assign the
derived class -- at your own risk.

--
Dieter

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor