473,379 Members | 1,337 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,379 software developers and data experts.

finding the parent class (not superclass) of the currently executingmethod derived from a Borg class

I want to create a class derived from a Borg class that can
instantiated as part of a script or be contained in other classes.
When methods from the Borg class are called, I would like to know the
name of the class that contains the Borg class.

I've played a bit with inspect and _getframe from the sys module but
get inconsistent results. The main problem is if the Borg class is
instantiated outside a containing class, then I need to go up a
different number of stack frames. But this information isn't
available till after I've run out of stack frames.

Hopefully the following code better describes what I'm looking to do.

import sys

class Borg:
_shared_state = {}
def __init__(self):
self.__dict__=self._shared_state

class Assimilated(Borg):
valueByCaller = {}

def __init__(self, setupvalue):
print "In Assimilated.__init__()"
print "setupvalue is: " + str(setupvalue)

# would like key to be name of class (or module) that
# contins Assimilated
callerID = sys._getframe(1).f_code.co_name

self.valueByCaller[callerID] = setupvalue

print self.valueByCaller

def action(self, calledvalue):
print "In Assimilated.action()"
print "self.__classname__: " + self.__class__.__name__
print "calledvalue is: " + str(calledvalue)

print "self.valueByCaller"
print self.valueByCaller

# need to get proper key depending on which class (or module)
# made the call
# print "0: " + sys._getframe(0).f_code.co_name
# print "1: " + sys._getframe(1).f_code.co_name
# print "2: " + sys._getframe(2).f_code.co_name
# print "3: " + sys._getframe(3).f_code.co_name
callerID = sys._getframe(2).f_code.co_name
print "callerID"
print callerID

if(self.valueByCaller[callerID] <= calledvalue):
print "doing the action"
class A:
assim_object = Assimilated(2)

def __init__(self):
self.assim_object.action(2)
self.assim_object.action(3)

class B:
assim_object = Assimilated(3)

def __init__(self):
self.assim_object.action(3)
self.assim_object.action(4)

class C:
assim_object = Assimilated(4)

def __init__(self):
self.assim_object.action(4)
self.assim_object.action(5)
a=A()
b=B()
c=C()

obj=Assimilated(3)
#obj.action(3)
When I run this, I get the following output:

In Assimilated.__init__()
setupvalue is: 2
{'A': 2}
In Assimilated.__init__()
setupvalue is: 3
{'A': 2, 'B': 3}
In Assimilated.__init__()
setupvalue is: 4
{'A': 2, 'C': 4, 'B': 3}
In Assimilated.action()
self.__classname__: Assimilated
calledvalue is: 2
self.valueByCaller
{'A': 2, 'C': 4, 'B': 3}
callerID
<module>
Traceback (most recent call last):
File "\CallerID.py", line 67, in <module>
a=A()
File "\CallerID.py", line 49, in __init__
self.assim_object.action(2)
File "\CallerID.py", line 41, in action
if(self.valueByCaller[callerID] <= calledvalue):
KeyError: '<module>'

What I found most peculiar when I started this was that the
valueByCaller dictionary was completely populated before the __init__
method of a was executed. I'm pretty sure that this has to do with
the difference between when the object gets instanced and when it gets
initialized, but I need to do some more research and reading to be
able to explain it to myself.

Thanks for any help you can give me.

Kevin
Sep 8 '08 #1
1 1087
seanacais a écrit :
I want to create a class derived from a Borg class that can
instantiated as part of a script or be contained in other classes.
When methods from the Borg class are called, I would like to know the
name of the class that contains the Borg class.
I've played a bit with inspect and _getframe from the sys module but
get inconsistent results. The main problem is if the Borg class is
instantiated outside a containing class, then I need to go up a
different number of stack frames. But this information isn't
available till after I've run out of stack frames.
The simplest solution is usually the better : explicitely pass the
caller (whether instance or module or whatever you want)

Hopefully the following code better describes what I'm looking to do.

import sys

class Borg:
_shared_state = {}
def __init__(self):
self.__dict__=self._shared_state

class Assimilated(Borg):
valueByCaller = {}
You understand that, being a class attribute, valueByCaller won't be
part of the Borg's _shared_state ?
def __init__(self, setupvalue):
print "In Assimilated.__init__()"
print "setupvalue is: " + str(setupvalue)

# would like key to be name of class (or module) that
# contins Assimilated
callerID = sys._getframe(1).f_code.co_name

self.valueByCaller[callerID] = setupvalue

print self.valueByCaller
Anyway, since you override __init__ and don't call Borg.__init__, your
Assimilated class doesn't behave as a Borg.

(snip)

>
When I run this, I get the following output:

In Assimilated.__init__()
setupvalue is: 2
{'A': 2}
In Assimilated.__init__()
setupvalue is: 3
{'A': 2, 'B': 3}
In Assimilated.__init__()
setupvalue is: 4
{'A': 2, 'C': 4, 'B': 3}
(snip)
>
What I found most peculiar when I started this was that the
valueByCaller dictionary was completely populated before the __init__
method of a was executed.
Indeed. In classes A, B and C, assim_object is class attribute - so it
is instanciated when the class statement is executed.
I'm pretty sure that this has to do with
the difference between when the object gets instanced and when it gets
initialized,
Not at all. It has to do with the fact that all statements within a
class block are executed before the class statement itself is executed.
And since your class statements are at the top-level, they are executed
when the module is initialised (that is, passed to the python runtime or
first imported).
but I need to do some more research and reading to be
able to explain it to myself.
Indeed. May I suggest that you *learn* Python's object model and
Python's execution model instead of assuming anything ? This will save
you a whole lot of time and frustration !-)
Sep 9 '08 #2

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

Similar topics

5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
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...
9
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
9
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. --...
11
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.