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

Printable string for 'self'

Is there a way to discover the original string form of the instance that
is represented by self in a method?

For example, if I have:

fred = C()
fred.meth(27)

then I would like meth to be able to print something like:

about to call meth(fred, 27) or
about to call fred.meth(27)

instead of:

about to call meth(<__main__.C instance at 0x00A9D238>, 27)

Thanks in advance,

Don.

Mar 15 '06 #1
4 1313
Don Taylor wrote:
Is there a way to discover the original string form of the instance that
is represented by self in a method?

For example, if I have:

fred = C()
fred.meth(27)

then I would like meth to be able to print something like:

about to call meth(fred, 27) or
about to call fred.meth(27)

instead of:

about to call meth(<__main__.C instance at 0x00A9D238>, 27)

Not a direct answer to your question, but this may be what you want:
If you give class C a __repr__ method you can avoid the default <class
instance at address> string.
class C(object): def __init__(self, name):
self.name = name
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.name)
def meth(self, y):
print 'about to call %r.meth(%r)' % (self, y)
fred = C('Fred')
fred.meth(27) about to call C('Fred').meth(27) def meth2(x, y): print 'about to call meth2(%r, %r)' % (x, y)
meth2(fred, 42) about to call meth2(C('Fred'), 42)
Of course, this doesn't tell you the name of the variable that points
to the instance. For that (here's your direct answer), you will have
to go to the source:
import inspect
import linecache
def f(a, b): print 'function call from parent scope:'
caller = inspect.currentframe().f_back
filename = caller.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, caller.f_lineno)
print ' ' + line.strip()
return a*b
fred = 4
x = f(3, fred)

function call from parent scope:
x = f(3, fred)
But defining __repr__ is far easier and more common.

--Ben

Mar 15 '06 #2
This behavior seems to be commonly wanted by people discovering Python,
and it is the rare case of something one can imagine that is really a
stretch to achieve in Python. Because more or less than one name may
refer to an object, in general an object can't know its name.

You can get part of the way there with

fred = C("fred")

and assign the string to self.name in the constructor. But you must
strictly enforce a convention in your code that the reference fred is
not reassigned to another referent for this to be of much use.

I got in some trouble in these parts a few months back for advocating
some sort of immutable reference, like

fred -> C("fred")

where any reassignment of the refernce during the lifetime of the
referent would raise an exception. This seems to be seen as wrongheaded
by greater pythonistas than myself. I don't fully understand *why*
this is a bad idea, but my intuitive idea that it would be very
valuable has gone away.

mt

Mar 16 '06 #3
Michael Tobis wrote:
I got in some trouble in these parts a few months back for advocating
some sort of immutable reference, like

fred -> C("fred")

where any reassignment of the refernce during the lifetime of the
referent would raise an exception. This seems to be seen as wrongheaded
by greater pythonistas than myself. I don't fully understand *why*
this is a bad idea, but my intuitive idea that it would be very
valuable has gone away.

fred = C("fred")
jim = fred RebindingError: cannot rebind <C object 'fred'> to name "jim" def showit(x): print x

showit(fred) RebindingError: cannot rebind <C object 'fred'> to name "x" fred.amethod() RebindingError: cannot rebind <C object 'fred'> to name "self" fred.aclassmethod() That works! sys.getrefcount(fred) RebindingError: cannot rebind <C object 'fred'> to name "object" del fred


What were you planning to do with this object exactly that didn't involve
binding it to any other names during its lifetime?
Mar 16 '06 #4
> What were you planning to do with this object exactly that didn't involve binding it to any other names during its lifetime?

Nothing so silly as that.

The idea is not to prevent other references from binding to the object,
but to allow the object to ensure that a certain symbol always points
to it.

I no longer suspect that this is a good enough idea to redesign the
language, but it seems to come up regularly.

I will elaborate on my use case later, as I am still looking for a
satisfactory workaround. I think that someone working on a logo-like
platform wanted to be able to assign permanent names to his turtles,
which is an easier case to understand.

In both cases there is the idea to construct a platform for programmers
of intermediate skill, to expose the python environment as well as
powerful objects that they don't need to modify, but need to be able to
access by name. It would be good to be able to guarantee that the name
would be a valid reference to the object in question.

mt

Mar 17 '06 #5

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

Similar topics

2
by: Jim | last post by:
Hi, Does anyone know of an available java class that will encrypt to cipher text consisting of only printable ascii characters? One of the things I need it for is to encrypt strings that will...
3
by: Pascal | last post by:
Hello, What's the best way to delete or replace no-printable characters in a string. i.e.: "\x08toto\x00titi" -> "tototiti" or " toto titi"
2
by: Daniel Alexandre | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there, I'm using the following method in my program to check whether a message received is printable or not and to strip the non-printable...
2
by: qazmlp | last post by:
How do you check whether an std::string object contains only printable characters ?
8
by: Sam Halliday | last post by:
i want to have a function which can print the printable form (possibly a 2 character string) of a character on UNIX like systems. for example, if i were to pass the ascii value '\3', i would like...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
6
by: Sandman | last post by:
Maybe it's me who is misunderstanding this, but isn't this string in quoted printable: "OLED & =?ISO-8859-1?Q?br=E4nsleceller?=" It's found in the Subject: header in a usenet message (i.e. not...
7
by: active | last post by:
I want to remove all non-printable characters - including nulls. I could extend the following by adding as many printable characters as I can think of. But I wonder if there isn't something...
4
by: John K Masters | last post by:
>From what I have read the string module is obsolete and should not be used but I am working on a project that parses printable files created in a DOS program and creates a web page for each file....
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.