473,811 Members | 3,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using an existing instance as parent

Hi, I was trying to find a way to set, upon __init__() the parent of a
class to an existing instance. Here is a minimal example of what I'm
trying to do:

class A(object):
def __init__(self, x):
self.x = x

class B(A):
def __init__(self, *args):
if not isinstance(args[0], A):
super(B, self).__init__( args[0])
else:
self = args[0]
self.y = args[1]

b = B(4, 6)
print 'b:', b.x, b.y, type(b)

a = A(7)
c = B(a, 3) # Means: please set c parent's using instance "a"
print 'c:', c.x, c.y, type(c)

This does not work as can be tested. The reason I'm in search for a
solution in this area is that in our project, "A" is not copy-able (it
is written using a boost.python binding to a C++ object that does not
allow copying) - so I can't simply call, inside "B's __init__()", a
copy constructor for A.

Any ideas?
Sep 1 '08 #1
1 2069
André a écrit :
Hi, I was trying to find a way to set, upon __init__() the parent of a
class to an existing instance.
??? Sorry but I just can't make any sense of this.
Here is a minimal example of what I'm
trying to do:

class A(object):
def __init__(self, x):
self.x = x

class B(A):
def __init__(self, *args):
if not isinstance(args[0], A):
super(B, self).__init__( args[0])
else:
self = args[0]
Rebinding an argument only affect the current namespace.
self.y = args[1]

b = B(4, 6)
print 'b:', b.x, b.y, type(b)

a = A(7)
c = B(a, 3) # Means: please set c parent's using instance "a"
print 'c:', c.x, c.y, type(c)

This does not work as can be tested. The reason I'm in search for a
solution in this area is that in our project, "A" is not copy-able (it
is written using a boost.python binding to a C++ object that does not
allow copying) - so I can't simply call, inside "B's __init__()", a
copy constructor for A.
Ok, I think I get the point... When an A instance is passed as first
arg, you'd like to use this instance's state to call super(B).__init __ ?

If so, here's a possible solution (Q&D, to be corrected according to
real use case, etc):

import copy

class B(A):
def __init__(self, *args):
if isinstance(args[0], A):
x = copy.copy(args[0].x) # if it's a mutable and you want a copy
else:
x = args[0]

super(B, self).__init__( x)
self.y = args[1]

Be sure to carefully read copy's doc.

Sep 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6828
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
16
2676
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
1
1964
by: clintonG | last post by:
How do I get a TreeNode.Parent property when using the 2.0 TreeView control? When the data source is an XML file there may be redundant names in the tree. For example, when a child node with the value of 'name' is selected I don't have a clue how to get the name of its parent which may be 'person' or 'school.' <%= Clinton Gallagher
8
16927
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
10
5117
by: John M. Gabriele | last post by:
The following short program fails: ----------------------- code ------------------------ #!/usr/bin/python class Parent( object ): def __init__( self ): self.x = 9 print "Inside Parent.__init__()"
5
8714
by: John Scott | last post by:
Ok..this a rather odd question/problem. I haven't really found a straight forward answer to how to handle this scenario, so I hope someone here can help. Here it is: I have an application that allows users to log in. Each user can have multiple roles within the application. Each role allows the user to access different sections of the app and allows/denies them certain functionality.
1
1546
by: sanou | last post by:
me again ok this is probably not possible... but let's say i have an explorer window open to www.google.com is there any way to reference that browser window...that particular instance? i've figured out how to have a parent window spawn a child window, have data entered into child window and then submit the data back to the parent window. communicating from parent to parent would be the missing piece and i have a sinking feeling...
53
4763
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
0
1737
by: =?Utf-8?B?Um9i?= | last post by:
I've a requirement to monitor when certain applications are started. I'm using WMI called from VS Stusio 2005 in VB to trap Excel and Word starting. I've written the following console application which wires up 4 events to trap Excel/Word Start/Stop events. The application works fine for Excel but Word behaves strangely. When I start an instance of Word I get the correct message displayed in the console. When I start a second instance of...
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.