[Tutor] What's the difference between %s and %r?

2011-07-23 Thread amt
Hello! I'm having troubles understanding what is the difference between %s
and %r(format characters). I did google  and found something on
StackOverflow but I don't understand the explanation as it's not beginner
orientated.


Also, I have this code from learn python the hard way. Why at line 9 does he
uses %r? Why did he didn't wrote print "I said: %s." %x ?

1x = "There are %d types of people." % 10
2binary = "binary"
3do_not = "don't"
4y = "Those who know %s and those who %s." % (binary, do_not)
5
6print x
7print y
8
9print "I said: %r." % x
10  print "I also said: '%s'." % y
11
12  hilarious = False
13  joke_evaluation = "Isn't that joke so funny?! %r"
14
15  print joke_evaluation % hilarious
16
17  w = "This is the left side of..."
18  e = "a string with a right side."
19
20  print w + e



Thanks in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread Wayne Werner
On Sat, Jul 23, 2011 at 7:48 AM, amt <0101...@gmail.com> wrote:

> Hello! I'm having troubles understanding what is the difference between %s
> and %r(format characters). I did google  and found something on
> StackOverflow but I don't understand the explanation as it's not beginner
> orientated.
>
>
> Also, I have this code from learn python the hard way. Why at line 9 does
> he uses %r? Why did he didn't wrote print "I said: %s." %x ?
> 


As the answer here (
http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python)
says,  it changes the method of evaluating an object. Here's a simple
example:


In [2]: class Thingy:
   ...: def __str__(self):
   ...: return "Hello"
   ...: def __repr__(self):
   ...: return "Goodbye"
   ...:
   ...:

In [3]: "%s %r" % (Thingy(), Thingy())
Out[3]: 'Hello Goodbye'

I'm not sure if there's a huge difference, and to be honest usually my
classes look like this:

class CoolGuy:
def __str__(self):
return "I'm a cool guy"
def __repr__(self):
return str(self)

so there would be no difference between the two.

In the interpreter,  the __repr__ method is called on the class when you
type it in - that's how python knows what to display:

In [5]: cool = Thingy()

In [6]: cool
Out[6]: Goodbye

Classes also start out with a default __repr__ that contains... well, this:

 In [7]: class AnotherThingy:
   ...: pass
   ...:

In [8]: neat = AnotherThingy()

In [9]: neat
Out[9]: <__main__.AnotherThingy instance at 0x9a5a66c>


HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread Lisi
On Saturday 23 July 2011 13:48:03 amt wrote:
> Hello! I'm having troubles understanding what is the difference between %s
> and %r(format characters). I did google  and found something on
> StackOverflow but I don't understand the explanation as it's not beginner
> orientated.
>
>
> Also, I have this code from learn python the hard way. Why at line 9 does
> he uses %r? Why did he didn't wrote print "I said: %s." %x ?
>
> 1x = "There are %d types of people." % 10
> 2binary = "binary"
> 3do_not = "don't"
> 4y = "Those who know %s and those who %s." % (binary, do_not)
> 5
> 6print x
> 7print y
> 8
> 9print "I said: %r." % x
> 10  print "I also said: '%s'." % y
> 11
> 12  hilarious = False
> 13  joke_evaluation = "Isn't that joke so funny?! %r"
> 14
> 15  print joke_evaluation % hilarious
> 16
> 17  w = "This is the left side of..."
> 18  e = "a string with a right side."
> 19
> 20  print w + e
>
>
>
> Thanks in advance!

I have recently worked through that exact question myself.  And it isn't well 
explained.

So - the simplistic answer, gleaned (hopefully not erroneously) from this 
list:  s means a string, d means a number and r can be either or both.  y has 
only words, so is a string, and x has a number (specifically referred to as 
d) and words, so needs r.

Lisi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread Martin A. Brown

Hello everybody,

 : > Hello! I'm having troubles understanding what is the difference between %s
 : > and %r(format characters). I did google  and found something on
 : > StackOverflow but I don't understand the explanation as it's not beginner
 : > orientated.
 : >
 : >
 : > Also, I have this code from learn python the hard way. Why at line 9 does
 : > he uses %r? Why did he didn't wrote print "I said: %s." %x ?
 : >
 : > 1x = "There are %d types of people." % 10
 : > 2binary = "binary"
 : > 3do_not = "don't"
 : > 4y = "Those who know %s and those who %s." % (binary, do_not)
 : > 5
 : > 6print x
 : > 7print y
 : > 8
 : > 9print "I said: %r." % x
 : > 10  print "I also said: '%s'." % y
 : > 11
 : > 12  hilarious = False
 : > 13  joke_evaluation = "Isn't that joke so funny?! %r"
 : > 14
 : > 15  print joke_evaluation % hilarious
 : > 16
 : > 17  w = "This is the left side of..."
 : > 18  e = "a string with a right side."
 : > 19
 : > 20  print w + e
 : >
 : >
 : >
 : > Thanks in advance!
 : 
 : I have recently worked through that exact question myself.  And 
 : it isn't well explained.
 : 
 : So - the simplistic answer, gleaned (hopefully not erroneously) 
 : from this list:  s means a string, d means a number and r can be 
 : either or both.  y has only words, so is a string, and x has a 
 : number (specifically referred to as d) and words, so needs r.

I am not horrendously well-versed here, but consider the mnemonic.

  %f   float
  %d   digit
  %s   string
  %r   representation

A representation is something that (might?) allow for some sort of 
round-trip, later (re)construction of the object.  A string is 
intended for general consumption.  Do you really need to distinguish 
them?  Only if you plan on re-consuming your own output, at which 
point you should consider the representation rather than the string.

There are doubtless more experienced hands here who will suggest 
concretely what you might do, but I would suggest that you use %s 
(string) for anything that you want to show to an end user and %r 
iif* you are planning to (re-)consume your own printed output.

-Martin

 * iif = if and only if

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread James Reynolds
I just use string{0}.format(arg) format and that solves needing to memorize
% whatevers.

On Sat, Jul 23, 2011 at 11:08 AM, Martin A. Brown wrote:

>
> Hello everybody,
>
>  : > Hello! I'm having troubles understanding what is the difference
> between %s
>  : > and %r(format characters). I did google  and found something on
>  : > StackOverflow but I don't understand the explanation as it's not
> beginner
>  : > orientated.
>  : >
>  : >
>  : > Also, I have this code from learn python the hard way. Why at line 9
> does
>  : > he uses %r? Why did he didn't wrote print "I said: %s." %x ?
>  : >
>  : > 1x = "There are %d types of people." % 10
>  : > 2binary = "binary"
>  : > 3do_not = "don't"
>  : > 4y = "Those who know %s and those who %s." % (binary, do_not)
>  : > 5
>  : > 6print x
>  : > 7print y
>  : > 8
>  : > 9print "I said: %r." % x
>  : > 10  print "I also said: '%s'." % y
>  : > 11
>  : > 12  hilarious = False
>  : > 13  joke_evaluation = "Isn't that joke so funny?! %r"
>  : > 14
>  : > 15  print joke_evaluation % hilarious
>  : > 16
>  : > 17  w = "This is the left side of..."
>  : > 18  e = "a string with a right side."
>  : > 19
>  : > 20  print w + e
>  : >
>  : >
>  : >
>  : > Thanks in advance!
>  :
>  : I have recently worked through that exact question myself.  And
>  : it isn't well explained.
>  :
>  : So - the simplistic answer, gleaned (hopefully not erroneously)
>  : from this list:  s means a string, d means a number and r can be
>  : either or both.  y has only words, so is a string, and x has a
>  : number (specifically referred to as d) and words, so needs r.
>
> I am not horrendously well-versed here, but consider the mnemonic.
>
>  %f   float
>  %d   digit
>  %s   string
>  %r   representation
>
> A representation is something that (might?) allow for some sort of
> round-trip, later (re)construction of the object.  A string is
> intended for general consumption.  Do you really need to distinguish
> them?  Only if you plan on re-consuming your own output, at which
> point you should consider the representation rather than the string.
>
> There are doubtless more experienced hands here who will suggest
> concretely what you might do, but I would suggest that you use %s
> (string) for anything that you want to show to an end user and %r
> iif* you are planning to (re-)consume your own printed output.
>
> -Martin
>
>  * iif = if and only if
>
> --
> Martin A. Brown
> http://linux-ip.net/
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread Joel Goldstick
On Sat, Jul 23, 2011 at 8:48 AM, amt <0101...@gmail.com> wrote:

> Hello! I'm having troubles understanding what is the difference between %s
> and %r(format characters). I did google  and found something on
> StackOverflow but I don't understand the explanation as it's not beginner
> orientated.
>
>
> Also, I have this code from learn python the hard way. Why at line 9 does
> he uses %r? Why did he didn't wrote print "I said: %s." %x ?
>
> 1x = "There are %d types of people." % 10
> 2binary = "binary"
> 3do_not = "don't"
> 4y = "Those who know %s and those who %s." % (binary, do_not)
> 5
> 6print x
> 7print y
> 8
> 9print "I said: %r." % x
> 10  print "I also said: '%s'." % y
> 11
> 12  hilarious = False
> 13  joke_evaluation = "Isn't that joke so funny?! %r"
> 14
> 15  print joke_evaluation % hilarious
> 16
> 17  w = "This is the left side of..."
> 18  e = "a string with a right side."
> 19
> 20  print w + e
>
>
>
> Thanks in advance!
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
r String (converts any python object using repr()). (5)

repr(*object*)¶ Return a
string containing a printable representation of an object. This is the same
value yielded by conversions (reverse quotes). It is sometimes useful to be
able to access this operation as an ordinary function. For many types, this
function makes an attempt to return a string that would yield an object with
the same value when passed to
eval(),
otherwise the representation is a string enclosed in angle brackets that
contains the name of the type of the object together with additional
information often including the name and address of the object. A class can
control what this function returns for its instances by defining a
__repr__() 
method.I
did some checking.  %r runs the repr() function on the object.  In this case
x is a string that needs to be evaluated.  So print "I said: %r." % x first
evaluates x then creates the full string.


-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Don't understand this class/constructor call syntax

2011-07-23 Thread dave
Thank you for the two explanations.  I think I have a good idea of what is
going on now with the arguments and keyword arguments.

My only remaining question is the pad_for_usrp argument.  The default value is
True so I thought it was a boolean and couldn't have anything to do with the
"self" that was passed to it.  However, I can probably puzzle
that out by looking at how it's used in the code.  

If you want to look at the full code and make any more comments, the code tree
is here: https://www.cgran.org/browser/projects/ucla_zigbee_phy/trunk/src

The example I'm looking at is (I quoted line 56):
https://www.cgran.org/browser/projects/ucla_zigbee_phy/trunk/src/examples/cc2420_txtest.py

The example relies on two files.  This one:
https://www.cgran.org/browser/projects/ucla_zigbee_phy/trunk/src/python/ieee802_15_4.py

And this one (I quoted the class at line 138):
https://www.cgran.org/browser/projects/ucla_zigbee_phy/trunk/src/python/ieee802_15_4_pkt.py

Thanks,
Dave


On Sat, 23 Jul 2011 13:09:07 +1000, Steven D'Aprano wrote
> dave wrote:
> 
> > class transmit_path(gr.top_block)
> [...]
> > self.packet_transmitter = 
> > ieee802_15_4_pkt.ieee802_15_4_mod_pkts(self,
> >   spb=self._spb, msgq_limit=2)
> 
> This calls the ieee802_15_4_mod_pkts initializer (not a constructor -
> - see below) with one positional argument and two keyword arguments.
> 
> The positional argument is "self", that is, the transmit_path instance.
> 
> The keyword arguments are called spb and msgq_limit; spb is set to 
> the value of self._spb, and msgq_limit is set to 2.
> 
> The reason I say this is an initializer and not a constructor is 
> that Python treats the two as different. The constructor that 
> creates the instance is called __new__ not __init__. When __init__ 
> is called, the instance has already been constructed, and is now 
> being initialized. The reason for this is mostly historical, 
> although it is useful.
> 
> (Disclaimer -- so called "old style" or "classic" classes don't have 
> a __new__ method, and you cannot customize the actual creation of 
> the instance, only the initialization.)
> 
> Looking at the ieee802_15_4_mod_pkts initializer:
> 
> > class ieee802_15_4_mod_pkts(gr.hier_block2):
> > def __init__(self, pad_for_usrp=True, *args, **kwargs):[/code]
> 
> As a method, this takes the instance as first argument (called 
> "self"), plus one named argument "pad_for_usrp", an arbitrary number 
> of unnamed positional arguments collected into "args", and an 
> arbitrary number of named keyword arguments collected into "kwargs".
> 
> (Note that args and kwargs are conventions. You could call them 
> anything you like -- the "magic", so to speak, comes from the 
> leading * and ** and not from the names.)
> 
> Given the call:
> 
> ieee802_15_4_mod_pkts(self, spb=self._spb, msgq_limit=2)
> 
> this corresponds to the initializer receiving arguments:
> 
> self = the freshly created ieee802_15_4_mod_pkts instance
> pad_for_usrp = the transmit_path instance doing the calling
> args = an empty tuple (no positional arguments collect)
> kwargs = a dictionary of keyword arguments
>   {'spb': value of _spb of the transmit_path instance,
>'msgq_limit': 2}
> 
> > What I don't understand is the call to the constructor and the constructor
> > definition.  Since it's using a number of advanced features, I'm having
> > trouble looking it all up in documentation.
> > 
> > What does it mean to call with spb=self._spb?  In the example file, spb is 
> > set
> > = to 2 and so is self._spb.  Is it a sort of pass by reference like C while
> > also assigning a value? Why the  ** on kwargs then? as if it is a matrix
> 
> No, this is nothing to do with pass by reference, or pass by value 
> either. This often confuses people coming to Python from some other 
> languages, and if it isn't a FAQ it ought to be. You can read one of 
> my posts on this here:
> 
> http://www.mail-archive.com/tutor%40python.org/msg46612.html
> 
> and the Wikipedia article:
> 
> http://en.wikipedia.org/wiki/Evaluation_strategy
> 
> What it means is that the method being called (in this case, 
> ieee802_15_4_mod_pkts.__init__) sees a keyword argument called 
> "spb". This keyword argument has name "spb", and value whatever 
> self._spb has at the time it is called.
> 
> When Python allocates arguments to the named parameters in a method 
> or function, its basic process is roughly something like this:
> 
> (1) for methods, automatically assign the instance being called to 
> the first named parameter (usually called "self" by convention);
> 
> (2) take each positional argument from the caller and assign it to 
> the remaining positional parameters, from left to right;
> 
> (3) assign any keyword arguments, raising an error if it duplicates 
> a value already seen;
> 
> (4) raise an error if any unassigned parameter doesn't have a 
> default value;
> 
> (5) collect any left over positional arguments

Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread wesley chun
%s = send object to str() first then drop it into the string
%r = send object to repr() first then drop it into the string

pretty straightforward:

>>> x = 'foo'
>>> str(x)
'foo'
>>> repr(x)
"'foo'"

why do people do %r at all? to get the quotes for free: :-)

>>> x = 'Python'
>>> print "What is this '%s' language?" % x
What is this 'Python' language?
>>> print "What is this %r language?" % x
What is this 'Python' language?

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.chun : wescpy-gmail.com : @wescpy
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the difference between %s and %r?

2011-07-23 Thread wesley chun
i forgot to define these:

str() - printable/human-readable string representation of an object
repr() - evaluatable string representation of an object (can "eval()"
it, meaning it is a string representation that evaluates to a Python
object)

in other words:

 x = 'foo'
 str(x)
> 'foo'
 repr(x)
> "'foo'"

eval(str(x)) is not a valid Python object (you'll get a NameError)
while eval(repr(x)) *is* a valid Python object (you'll get a string
'foo'):

>>> eval('foo')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name 'foo' is not defined
>>> eval("'foo'")
'foo'

-wesley


On Sat, Jul 23, 2011 at 12:06 PM, wesley chun  wrote:
> %s = send object to str() first then drop it into the string
> %r = send object to repr() first then drop it into the string
> pretty straightforward:
 x = 'foo'
 str(x)
> 'foo'
 repr(x)
> "'foo'"
> why do people do %r at all? to get the quotes for free: :-)
 x = 'Python'
 print "What is this '%s' language?" % x
> What is this 'Python' language?
 print "What is this %r language?" % x
> What is this 'Python' language?


-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Python Web Development with Django", Addison Wesley, (c) 2009
    http://withdjango.com

wesley.chun : wescpy-gmail.com : @wescpy
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Don't understand this class/constructor call syntax

2011-07-23 Thread Alan Gauld

dave wrote:


My only remaining question is the pad_for_usrp argument.  The default value is
True so I thought it was a boolean and couldn't have anything to do with the
"self" that was passed to it.  However, I can probably puzzle
that out by looking at how it's used in the code.  


I thought that was weird too and my first thought was that it was a bug.
The I thought that maybe it was using the fact that an object in Python 
normally evaluates to True so maybe the code tests for True and if True 
tries to determine the type and perform some operation.


If that's what its doing I don't like it and think it's a bad design but 
it is feasible.


OTOH it may still just be a bug! :-)

Alan G



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Copying Variables

2011-07-23 Thread Ryan Strunk
Hello everyone,
How can I make two copies of a dictionary that don't point to the same
location in memory? My plan is to generate d1 and make d2 a copy of d1.
After the user modifies d1 I want him/her to be able to return to the
initial dictionary (d2) values. I tried:
d1 = {values}
d2 = dict(d1)
then later in the code when I want to re-initialize d1:
d1 = dict(d2)
but this won't work. Any suggestions you have as to how I can make this work
are welcome.
Best,
Ryan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying Variables

2011-07-23 Thread Steven D'Aprano

Ryan Strunk wrote:

Hello everyone,
How can I make two copies of a dictionary that don't point to the same
location in memory? My plan is to generate d1 and make d2 a copy of d1.
After the user modifies d1 I want him/her to be able to return to the
initial dictionary (d2) values. I tried:
d1 = {values}


That can't work, because that makes a set, not a dict (in Python 3 at 
least). Perhaps you mean {key: value}, not just {value}?



d2 = dict(d1)
then later in the code when I want to re-initialize d1:
d1 = dict(d2)
but this won't work. Any suggestions you have as to how I can make this work
are welcome.


Define "this won't work". What makes you think it doesn't work? This 
makes d2 a copy of d1, then makes d1 a copy of d2. They are different dicts.


My *guess* is that you're modifying the objects *inside* d1 and d2, 
which are shared. To make copies of *everything*, all the way down, use 
the copy module:


copy.copy  # makes a shallow copy, one level only
copy.deepcopy  # makes a deep copy, all the way down


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question related to Tkinker

2011-07-23 Thread Emeka
Hello All,

I am putting up a simple game .. the game is about manipulation. If the gets
through level one ... I have to change the word with another...

Am I going to destroy level window and build level 2 or is there a way to
just adjust the word (I used labels)

Regards,
Janus

-- 
*Satajanus  Nig. Ltd


*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor