Re: Printable string for 'self'
> 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
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 to name "jim" >>> def showit(x): print x >>> showit(fred) RebindingError: cannot rebind to name "x" >>> fred.amethod() RebindingError: cannot rebind to name "self" >>> fred.aclassmethod() That works! >>> sys.getrefcount(fred) RebindingError: cannot rebind 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? -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Don Taylor a écrit : (snip) > My overall intent is to try to build something that can record > interactions against an object so that they can be replayed later for > testing and debugging. I had in mind to generate the recording as a > sequence of Python statements. You may want to have a look at the command and memento patterns. -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Fredrik Lundh wrote: > Q. How can my code discover the name of an object? > > A. The same way as you get the name of that cat you found on your > porch: the cat itself cannot tell you its name, and it doesn't really > care -- so the only way to find out what it's called is to ask all your > neighbours if it's their cat... and don't be surprised if you'll find that > it's known by many names, or no name at all! > > from: > > > http://www.python.org/doc/faq/programming.html#how-can-my-code-discover-the-name-of-an-object > > (fwiw, the current crop of stray cats in my neighbourhood are known > as "sune" and "the big fat red one") I see. "This is a project on which we have been working for the last three centuries since the lamasery was founded, in fact. It is somewhat alien to your way of thought, so I hope you will listen with an open mind while I explain it." "Naturally." "It is really quite simple. We have been compiling a list which shall contain all the possible names of God." - From "The Nine Billion Names of God" by Arthur C. Clarke. http://www.geocities.com/rojodos/docs/90.htm > no, as long as you're aware that you're doing introspection, and that your > code won't run in all Python environments. Yes, it is introspection for a testing tool - not for production code. Thanks, Don. -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Don Taylor wrote: > Fredrik Lundh wrote: > >>objects don't have names in Python, and the source is not part of >>the running program. >> >>have you read this ? >> >>http://effbot.org/zone/python-objects.htm > > > I have now. Thank you very much. > > "objects don't have names in Python": It appears from the code that > Michael posted that objects can discover the names that are bound to > themselves. Is this true in general? If so, then I guess it does not > matter which name I use as long as it is bound to the object. > > "the source is not part of the running program" : Ok, but in my case I > would have the source that corresponds to the running program available > to me and the inspect module does appear to provide enough information > for me to find the corresponding piece of the source code. Is there > something wrong with using the inspect module for this sort of work? > > My overall intent is to try to build something that can record > interactions against an object so that they can be replayed later for > testing and debugging. I had in mind to generate the recording as a > sequence of Python statements. I would like to do this without > modifying the source of the target class. At the moment this is just a > project to help me learn Python, although it would nice if it did yield > something useful. > > Is there anything around that already does this sort of thing? > Nothing springs to mind. However, given your statement of the overall intent, I should say that there's simply no need to record the "names" of the objects that are being modified. Objects can indeed discover the names to which they are bound, albeit by a rather tortuous process of namespace introspection, but I don't see what advantages this offers in terms of how a given program modifies its environment (or namespace). Surely the important thing is that objects are created, modified in various ways, and finally destroyed? If you are hoping to record "interactions against an object" without modifying the object's implementation I suspect your efforts may be doomed to failure. This opinion is offered mostly to motivate the many wrong-headed and knowledgeable individuals who would gain pleasure from proving my assertion incorrect and thereby helping you to solve your problem. Good luck. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd www.holdenweb.com Love me, love my blog holdenweb.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Don Taylor wrote: > Fredrik Lundh wrote: > > objects don't have names in Python, and the source is not part of > > the running program. > > > > have you read this ? > > > > http://effbot.org/zone/python-objects.htm > > I have now. Thank you very much. > > "objects don't have names in Python": It appears from the code that > Michael posted that objects can discover the names that are bound to > themselves. Is this true in general? to the extent they have names, and the names exist in a namespace that you can reach. it might be time to quote myself here: Q. How can my code discover the name of an object? A. The same way as you get the name of that cat you found on your porch: the cat itself cannot tell you its name, and it doesn't really care -- so the only way to find out what it's called is to ask all your neighbours if it's their cat... and don't be surprised if you'll find that it's known by many names, or no name at all! from: http://www.python.org/doc/faq/programming.html#how-can-my-code-discover-the-name-of-an-object (fwiw, the current crop of stray cats in my neighbourhood are known as "sune" and "the big fat red one") > "the source is not part of the running program" : Ok, but in my case I > would have the source that corresponds to the running program available > to me and the inspect module does appear to provide enough information > for me to find the corresponding piece of the source code. Is there > something wrong with using the inspect module for this sort of work? no, as long as you're aware that you're doing introspection, and that your code won't run in all Python environments. -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Fredrik Lundh wrote: > objects don't have names in Python, and the source is not part of > the running program. > > have you read this ? > > http://effbot.org/zone/python-objects.htm I have now. Thank you very much. "objects don't have names in Python": It appears from the code that Michael posted that objects can discover the names that are bound to themselves. Is this true in general? If so, then I guess it does not matter which name I use as long as it is bound to the object. "the source is not part of the running program" : Ok, but in my case I would have the source that corresponds to the running program available to me and the inspect module does appear to provide enough information for me to find the corresponding piece of the source code. Is there something wrong with using the inspect module for this sort of work? My overall intent is to try to build something that can record interactions against an object so that they can be replayed later for testing and debugging. I had in mind to generate the recording as a sequence of Python statements. I would like to do this without modifying the source of the target class. At the moment this is just a project to help me learn Python, although it would nice if it did yield something useful. Is there anything around that already does this sort of thing? Cheers, Don. -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Don Taylor wrote: > And if I call my method: A().qualify global_names_bound_to_me() then I > get an empty list back. > > Is what I want to do not possible without referring back to the source? objects don't have names in Python, and the source is not part of the running program. have you read this ? http://effbot.org/zone/python-objects.htm -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
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 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
"Don Taylor" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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) That last *is* the default 'original string form of the instance'. Consider >>> [C() for i in [1,2,3]] [<__main__.C object at 0x00B536F0>, <__main__.C object at 0x00B53D90>, <__main__.C object at 0x00B53830>] If you want your instances to have a name attribute, as with functions, classes, and modules, then give them one and supply it to the init method. Note that it does not have to be a bindable name, just a string. IE, 'a[1]' could be the string identifier. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
Michael Spencer wrote: > > > In general, this requires exhaustive search of name bindings e.g.,: > > >>> def get_names_of(obj, ns): > ... return [name for name, value in ns.iteritems() if value is obj] > ... > >>> class A(object): > ... def global_names_bound_to_me(self): > ... return get_names_of(self, globals()) > ... > >>> a = A() > >>> a.global_names_bound_to_me() > ['a'] > >>> b = a > >>> a.global_names_bound_to_me() > ['a', 'b'] > >>> > Ah, ok. But, as you show in the example, this technique does not let me figure out which of 'a' or 'b' was used to qualify global_names_bound_to_me(). And if I call my method: A().qualify global_names_bound_to_me() then I get an empty list back. Is what I want to do not possible without referring back to the source? Don. -- http://mail.python.org/mailman/listinfo/python-list
Re: Printable string for 'self'
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) > > Thanks in advance, > > Don. > In general, this requires exhaustive search of name bindings e.g.,: >>> def get_names_of(obj, ns): ... return [name for name, value in ns.iteritems() if value is obj] ... >>> class A(object): ... def global_names_bound_to_me(self): ... return get_names_of(self, globals()) ... >>> a = A() >>> a.global_names_bound_to_me() ['a'] >>> b = a >>> a.global_names_bound_to_me() ['a', 'b'] >>> HTH Michael -- http://mail.python.org/mailman/listinfo/python-list
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. -- http://mail.python.org/mailman/listinfo/python-list