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

Introspection Class/Instance Name

Hello there,

what method would you use to return the name of the class and/or
instance introspectively eg.

class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()
instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'

I have been scouring the 2.4 docs ... I am sure it is frustratingly simple

thx in advance

**
Apr 26 '06 #1
4 2660
*binarystar* wrote:
Hello there,

what method would you use to return the name of the class and/or
instance introspectively eg.

class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()
instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'

I have been scouring the 2.4 docs ... I am sure it is frustratingly simple

thx in advance

**


Hi,

take a look at self.__class__.__name__ for the Class-name.

HtH, Roland
Apr 26 '06 #2
What about:

py> class A:
py. def __init__(self):
py. print self.__class__.__name__
py. print str(self)
py. def __str__(self):
py. return 'instance of %s' % self.__class__.__name__
py.
py> a = A()
A
instance of A
py>

Apr 26 '06 #3
*binarystar* wrote:
class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()
instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'

class Bollocks(object): def name_of_instance(self):
return "self"

instance_of_bollocks = Bollocks()
print instance_of_bollocks.__class__.__name__ Bollocks print instance_of_bollocks.name_of_instance() self
At the time when the method is called, 'self' is a perfectly valid name for
the instance. Seriously though, how do you expect a method to decide if you
do:
another_name = instance_of_bollocks
print another_name.name_of_instance() ??? which name should appear here ??? more = [another_name]*5
print more[2] ??? and what name here ???

and did you want a global name, or a local variable from some function and
if so which function and at which stack level?

Python does actually give you sufficient introspection to do this, but you
have to write some fairly complex code to iterate through the namespaces
you are interested in searching for the object.

A much more reasonable solution is to give your object a name attribute:
class Bollocks(object): def __init__(self, name):
self.name = name

instance_of_bollocks = Bollocks('Archimedes')
print instance_of_bollocks.name

Archimedes
Apr 26 '06 #4
*binarystar* wrote:
Hello there,

what method would you use to return the name of the class and/or
instance introspectively eg.

class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()
instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'


self.__class__ is good for getting instance's top class

yet Python is weak on introspecting the namespace of its definitions -
funcs and classes. it stops at sys._getframe().f_code.co_name

thus, e.g. for things like super() you need always to re-type the class
name where you just write in!?

maybe Py3K brings more ? Maybe There could be compiler-variables like
__CLASS__ , __FUNC__ for things like super(), recursion etc.
(compare http://aspn.activestate.com/ASPN/Coo.../Recipe/491265 )

-robert
Apr 26 '06 #5

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

Similar topics

1
by: Rim | last post by:
Hi, Is there a way to get any sort of introspection which would return the content of an instance *in the order they were declared or executed* as opposed to alphabetical order? Example: ...
2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
8
by: mirandacascade | last post by:
Where in the language would one find the intropsection capability to answer the question: what class am I in? Example: class ExistentialCrisis: def __init__(self, text): self.spam = text...
0
by: Steven T. Hatton | last post by:
I suspect the core language is not the level at which introspection should be implemented in C++. That has been the choice of C#, and Java. Both of these languages made some trade-offs to...
2
by: greg.corson | last post by:
Hi, I'm in the process of building a complex system using a sort of "system services" model. I'm looking at putting together various services like serialization, networking, 2D/3D rendering,...
6
by: jsceballos | last post by:
Hello. I'm writing a proxy class, i.e: a class whose methods mostly delegate their functionality to other class object. Most of the methods (which are quite a lot) defined in the class would end...
3
by: James Stroud | last post by:
Hello, I wanted to automagically generate an instance of a class from a dictionary--which might be generated from yaml or json. I came up with this: # automagical constructor def...
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.