Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why does Python show the whole array?

Reply
Thread Tools

Why does Python show the whole array?

 
 
Niklas Norrthon
Guest
Posts: n/a
 
      04-09-2009
On 9 Apr, 09:49, Miles <(E-Mail Removed)> wrote:
> On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote:
> > Lawrence D'Oliveiro wrote:
> >> This is why conditional constructs should not accept any values other than
> >> True and False.

>
> > So you think

>
> > if test.find(item) == True: ...

>
> > would have been better?

>
> Clearly, any comparison with a boolean literal should be illegal. *
>


So you think

truth_value = True
if test.find(item) == truth_value: ...

would have been better?
(couldn't resist...)

/Niklas Norrthon

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-09-2009
On Thu, 09 Apr 2009 18:53:13 +1200, Lawrence D'Oliveiro wrote:

> This is why conditional constructs should not accept any values other
> than True and False.


I once tried this:

for i in alist.sort():

and got an error I didn't understand because I failed to read the docs.
Clearly for loops shouldn't take sequences but only an integer count,
because that would have prevented my error.



--
Steven
 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-09-2009
In message <7e7a386f-d336-4186-822d-
http://www.velocityreviews.com/forums/(E-Mail Removed)>, John Machin wrote:

> On Apr 9, 4:53 pm, Lawrence D'Oliveiro <l...@geek-
> central.gen.new_zealand> wrote:
>
>> This is why conditional constructs should not accept any values other
>> than True and False.

>
> An alternative viewpoint: This is what happens when one user doesn't
> understand a long-standing idiom and/or doesn't read the manual.
> That's all. It doesn't constitute evidence that such a restriction
> would be generally beneficial.


Fine if it only happened once. But it's a commonly-made mistake. At some
point you have to conclude that not all those people are stupid, there
really is something wrong with the design.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      04-09-2009
Lawrence D'Oliveiro wrote:
> In message <(E-Mail Removed)>, Gilles Ganault
> wrote:
>
>> test = "(E-Mail Removed)"
>> isp = ["gmail.com", "yahoo.com"]
>> for item in isp:
>> if test.find(item):
>> print item
>> ======= output
>> gmail.com
>> yahoo.com
>> =======

>
> This is why conditional constructs should not accept any values other than
> True and False.


Nonesense. Just use 'if item in test:' which tells you all the info you
are using. str.find returns an index that you ignore, so don't use it.

 
Reply With Quote
 
John Posner
Guest
Posts: n/a
 
      04-09-2009
Lawrence D'Oliveiro wrote:

> Fine if it only happened once. But it's a commonly-made mistake. At some
> point you have to conclude that not all those people are stupid, there
> really is something wrong with the design.


I think "something wrong with the design" is overstating the case a bit,
and is likely to elicit some negative reactions in this forum. But I
agree with your point, to this extent: this situation illustrates the
way in which Python "overloads" the number zero:

Realm A: "0" indicates the first position in a sequence
Realm B: "0" indicates the Boolean value "False"

You just need to remember that the find() function works in Realm A, and
the "in" operator works in Realm B.

Q: Has anyone on the python-dev list ever proposed a "string"-module
function that does the job of the "in" operator? Maybe this:

if test.contains(item) # would return a Boolean value

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      04-09-2009
On Apr 10, 12:35*am, John Posner <(E-Mail Removed)> wrote:
> Lawrence D'Oliveiro wrote:
>
> *> Fine if it only happened once. But it's a commonly-made mistake. At some
> *> point you have to conclude that not all those people are stupid, there
> *> really is something wrong with the design.
>
> I think "something wrong with the design" is overstating the case a bit,
> and is likely to elicit some negative reactions in this forum. But I
> agree with your point, to this extent: this situation illustrates the
> way in which Python "overloads" the number zero:
>
> * Realm A: *"0" indicates the first position in a sequence
> * Realm B: * "0" indicates the Boolean value "False"
>
> You just need to remember that the find() function works in Realm A, and
> the "in" operator works in Realm B.
>
> Q: Has anyone on the python-dev list ever proposed a "string"-module
> function that does the job of the "in" operator? Maybe this:
>
> * if test.contains(item) * * # would return a Boolean value


Yes.

command_prompt> \python23\python -c "print 'frobozz'.__contains_
_('obo')"
True

Was first introduced in Python 2.2, with only a single character
allowed in the arg.
 
Reply With Quote
 
Tim Rowe
Guest
Posts: n/a
 
      04-09-2009
2009/4/9 Miles <(E-Mail Removed)>:

> Clearly, any comparison with a boolean literal should be illegal. *


Hey, we could have strict type checking at compile time of /all/
operations, couldn't we? Anybody care to join me over at the Ada list?


--
Tim Rowe
 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      04-09-2009
John Posner <(E-Mail Removed)> writes:

> Q: Has anyone on the python-dev list ever proposed a "string"-module
> function that does the job of the "in" operator? Maybe this:
>
> if test.contains(item) # would return a Boolean value


That's a string method, not a function in the string module. If you
want a function, use operator.contains:

>>> import operator
>>> operator.contains('foo', 'o')

True
 
Reply With Quote
 
John Posner
Guest
Posts: n/a
 
      04-09-2009
Hrvoje Niksic wrote:

> if test.contains(item) # would return a Boolean value
>


>> That's a string method, not a function in the string module.


Oops, of course.

>>>> import operator
>>>> operator.contains('foo', 'o')


That's pretty good, and IMHO a bit better than John Machin's suggestion
to use the __contains__() method. (I have this prejudice that using the
__XXX__ methods in "everyday code" is cheating.)

Given how common string maniuplations are, I guess I'm surprised that
Python hasn't yet made "contains()" into both a "string"-module function
*and* a string-object method.


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-09-2009
John Posner wrote:

> Given how common string maniuplations are, I guess I'm surprised that
> Python hasn't yet made "contains()" into both a "string"-module function
> *and* a string-object method.


Could you explain why you prefer 'contains(belly, beer)'
or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit
harder to find in the documentation, but once a newbie has learned about it
he'll find it easy to remember.

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does the crypto key show in "show run" on some switches and notothers? ttripp Cisco 2 10-19-2010 08:45 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
*WITHOUT* using: ValidateRequest="False" for the whole page (or my whole site).... \A_Michigan_User\ ASP .Net 2 08-21-2006 02:13 PM
why does the show function not show anything? greenflame Javascript 6 06-18-2005 08:57 PM



Advertisments