472,328 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 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 1030
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...
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....
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...
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...
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...
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 {...
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...
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...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.