No doubt I've overlooked something obvious, but here goes:
Let's say I assign a value to a var, e.g.:
myPlace = 'right here'
myTime = 'right now'
Now let's say I want to print out the two vars, along with their names.
I could easily do this:
print "myPlace = %s, myTime = %s" % (myPlace, myTime)
[EMAIL PROTECTED] said unto the world upon 2005-03-29 01:06:
No doubt I've overlooked something obvious, but here goes:
Let's say I assign a value to a var, e.g.:
myPlace = 'right here'
myTime = 'right now'
Now let's say I want to print out the two vars, along with their names.
I could easily do t
[EMAIL PROTECTED] wrote:
> No doubt I've overlooked something obvious, but here goes:
>
> Let's say I assign a value to a var, e.g.:
> myPlace = 'right here'
> myTime = 'right now'
>
> Now let's say I want to print out the two vars, along with their
names.
> I could easily do this:
> print "myPlac
On 28 Mar 2005 22:06:44 -0800, rumours say that
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> might have
written:
Read about locals() and globals() in the Python documentation. These
provide the information you request (ie what names are bound to what
objects).
--
TZOTZIOY, I speak England very best
On 28 Mar 2005 23:01:34 -0800, "Dan Bishop" <[EMAIL PROTECTED]> wrote:
def print_vars(vars_dict=None):
>...if vars_dict is None:
>... vars_dict = globals()
>...for var, value in vars_dict.items():
>... print '%s = %r' % (var, value)
>...
myPlace = 'right here'
my
On Tue, 29 Mar 2005 14:34:39 GMT, Ron_Adam <[EMAIL PROTECTED]> wrote:
> On 28 Mar 2005 23:01:34 -0800, "Dan Bishop" <[EMAIL PROTECTED]> wrote:
>
> def print_vars(vars_dict=None):
> >...if vars_dict is None:
> >... vars_dict = globals()
> >...for var, value in vars_dict.items():
In article <[EMAIL PROTECTED]>,
Bill Mill <[EMAIL PROTECTED]> wrote:
.
.
.
>(i.e. I respectfully disagree that mixing data with program code is a bad idea)
.
.
On Tue, 29 Mar 2005 18:08:04 GMT, Cameron Laird <[EMAIL PROTECTED]> wrote:
> In article <[EMAIL PROTECTED]>,
> Bill Mill <[EMAIL PROTECTED]> wrote:
> .
> .
> .
> >(i.e. I respectfully disagree that mixing data with program cod
On Tue, 29 Mar 2005 11:23:45 -0500, Bill Mill <[EMAIL PROTECTED]>
wrote:
>On Tue, 29 Mar 2005 14:34:39 GMT, Ron_Adam <[EMAIL PROTECTED]> wrote:
>> On 28 Mar 2005 23:01:34 -0800, "Dan Bishop" <[EMAIL PROTECTED]> wrote:
>>
>> def print_vars(vars_dict=None):
>> >...if vars_dict is None:
>>
> >>
> >> Fred = 5
> >> John = 8
> >> Winner = John
> >>
> >> Both John and Winner are pointing to the literal '8'.
> >
> >ummm, yes, of course they are. What's your point?
>
> Hi Bill,
>
> My point is if you look up the name and print it, you may get.
>
> Instead of:
>
> Fred has 5 points
On Tue, 29 Mar 2005 14:58:45 -0500, Bill Mill <[EMAIL PROTECTED]>
wrote:
>>
>> Or something else depending on how many references you made to the
>> value 8.
>
>Yes, this is true, assuming that he looks for keys with the value 8 in
>locals(). It's not necessarily true if there's a way to ask py
Ron_Adam wrote:
> I've been playing around with a way to explore name spaces, but once
> you drop into class's, and functions, the references can lead you into
> an endless loops.
>
Here is a rough attempt at printing the names of a variable. It will pick
up several names where appropriate, but
my god, I've created a monster!
Maybe I should restate my original problem. Actually, the word
'problem' is too strong. I had a little curiosity about whether I could
write a couple of lines of code more succinctly, or more pythonically.
I didn't realize that this would trigger a discussion abou
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> Restating: I'm doing some debugging of some code. I want to print out
> the value of two variables whose names are known. Let's call them
> myTime and myPlace.
>
> #debug:
> if self.debug:
>print "myTime = %s, myPlace = %s" % (myTime, myPlac
thanks Aaron, I'll pick what's behind door no. 1 !
That is, I liked your first solution. Having said that, I would only
have to place either solution once in my code, then I could call it
again and again. So either one would be just as 'light' in actual
usage.
Taking this idea a little further,
[EMAIL PROTECTED] wrote:
> But surely if you create an integer object and assign it a value, e.g.
> a = 3,
> why shouldn't Python be able to tell you something like the following:
> name(a) >>> 'a'
> ?
But why should it return 'a' and not one of these?
tokenize.tok_name[3]
token.tok_name[3]
sre
On 30 Mar 2005 21:56:06 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>my god, I've created a monster!
>
>Maybe I should restate my original problem. Actually, the word
>'problem' is too strong. I had a little curiosity about whether I could
>write a couple of lines of code more succinctly
On 31 Mar 2005 08:13:30 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
>
> > But surely if you create an integer object and assign it a value, e.g.
> > a = 3,
> > why shouldn't Python be able to tell you something like the following:
> > name(a) >>> 'a'
> > ?
>
> But why
On Thu, 31 Mar 2005 03:33:10 -0500, Bill Mill <[EMAIL PROTECTED]> wrote:
> On 31 Mar 2005 08:13:30 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote:
> > [EMAIL PROTECTED] wrote:
> >
> > > But surely if you create an integer object and assign it a value, e.g.
> > > a = 3,
> > > why shouldn't Python be ab
Stewart Midwinter <[EMAIL PROTECTED]> writes:
[snip]
> Taking this idea a little further, I'd like to build a 'variable
> inspector' for my GUI app, so that I don't have to resort to debug
> statements. Instead, I could pop open a window that lists every
> variable that has an expected None, str
On 30 Mar 2005 08:43:17 GMT, Duncan Booth
<[EMAIL PROTECTED]> wrote:
>Here is a rough attempt at printing the names of a variable. It will pick
>up several names where appropriate, but deliberately doesn't attempt to
>get all possible names (as you say, that could result in endless loops).
>In par
Bengt wrote:
"The way you use those words makes me wonder: "assign _it_"?? Which
'it'? "
- it's probably evident to any close observer that my understanding of
objects is superficial, no doubt a reflection of the fact that I
started programming on punch cards a very long time ago. I use objects
e
On 31 Mar 2005 20:54:42 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>Bengt wrote:
>"The way you use those words makes me wonder: "assign _it_"?? Which
>'it'? "
>
>- it's probably evident to any close observer that my understanding of
>objects is superficial, no doubt a reflection of the
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
[...]
> Restating: I'm doing some debugging of some code. I want to print out
> the value of two variables whose names are known. Let's call them
> myTime and myPlace.
[...]
Why not simply get your editor to insert the variable name twice? I
have
24 matches
Mail list logo