473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2044
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
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...
16
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...
1
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...
8
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. ...
10
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...
5
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...
1
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? ...
53
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,...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...

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.