Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
len() should always return something
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 60 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dr. Phillip M. Feldman  
View profile  
 More options Jul 24 2009, 2:35 am
Newsgroups: comp.lang.python
From: "Dr. Phillip M. Feldman" <pfeld...@verizon.net>
Date: Thu, 23 Jul 2009 23:35:43 -0700 (PDT)
Local: Fri, Jul 24 2009 2:35 am
Subject: len() should always return something

Some aspects of the Python design are remarkably clever, while others leave
me perplexed. Here's an example of the latter: Why does len() give an error
when applied to an int or float? len() should always return something; in
particular, when applied to a scalar, it should return a value of 1. Of
course, I can define my own function like this:

def mylen(x):
   if isinstance(x,int) or isinstance(x,float): return 1
   return len(x)

But, this shouldn't be necessary.
--
View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp2463...
Sent from the Python - python-list mailing list archive at Nabble.com.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Jul 24 2009, 2:57 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Fri, 24 Jul 2009 08:57:44 +0200
Local: Fri, Jul 24 2009 2:57 am
Subject: Re: len() should always return something
Dr. Phillip M. Feldman schrieb:

> Some aspects of the Python design are remarkably clever, while others leave
> me perplexed. Here's an example of the latter: Why does len() give an error
> when applied to an int or float? len() should always return something; in
> particular, when applied to a scalar, it should return a value of 1. Of
> course, I can define my own function like this:

> def mylen(x):
>    if isinstance(x,int) or isinstance(x,float): return 1
>    return len(x)

> But, this shouldn't be necessary.

Can you show some example of where that is actually making a piece of
code more elegant?

Diez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Jul 24 2009, 3:02 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Fri, 24 Jul 2009 00:02:28 -0700
Local: Fri, Jul 24 2009 3:02 am
Subject: Re: len() should always return something
On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M.

Feldman<pfeld...@verizon.net> wrote:

> Some aspects of the Python design are remarkably clever, while others leave
> me perplexed. Here's an example of the latter: Why does len() give an error
> when applied to an int or float? len() should always return something; in
> particular, when applied to a scalar, it should return a value of 1. Of
> course, I can define my own function like this:

> def mylen(x):
>   if isinstance(x,int) or isinstance(x,float): return 1
>   return len(x)

> But, this shouldn't be necessary.

The problem is that redefining len()/length/size that way would
violate several principles of Python's design (The "Zen" of Python -
http://www.python.org/dev/peps/pep-0020/).

Specifically:
- Explicit is better than implicit.
- Special cases aren't special enough to break the rules.
- Errors should never pass silently.
- In the face of ambiguity, refuse the temptation to guess.

If you'd explain the situation that prompts you to find this
redefinition necessary, I'm sure someone can suggest a better
approach.

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Jul 24 2009, 3:07 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Fri, 24 Jul 2009 09:07:03 +0200
Local: Fri, Jul 24 2009 3:07 am
Subject: Re: len() should always return something
Dr. Phillip M. Feldman wrote:

> Some aspects of the Python design are remarkably clever, while others
> leave me perplexed. Here's an example of the latter: Why does len() give
> an error when applied to an int or float? len() should always return
> something; in particular, when applied to a scalar, it should return a
> value of 1. Of course, I can define my own function like this:

> def mylen(x):
>    if isinstance(x,int) or isinstance(x,float): return 1
>    return len(x)

> But, this shouldn't be necessary.

Python should not blur the distinction between vectors an scalars like that.
Instead of trying to be clever you should pass a vector with a single item
and send mylen() to /dev/null.

On a general note, I think one of Python's strengths is that it consistently
/avoids/ this kind of cleverness.

A prominent example is the handling of "1" + 1.

Peter


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Edwards  
View profile  
 More options Jul 24 2009, 9:57 am
Newsgroups: comp.lang.python
From: Grant Edwards <invalid@invalid>
Date: Fri, 24 Jul 2009 08:57:02 -0500
Local: Fri, Jul 24 2009 9:57 am
Subject: Re: len() should always return something
On 2009-07-24, Dr. Phillip M. Feldman <pfeld...@verizon.net> wrote:

> Some aspects of the Python design are remarkably clever, while
> others leave me perplexed. Here's an example of the latter:
> Why does len() give an error when applied to an int or float?
> len() should always return something; in particular, when
> applied to a scalar, it should return a value of 1.

If len(7) returned a value of 1, then wouldn't one expect 7[0]
to be valid?  It isn't, so you'd then have to redefine all
types so that they are sequences that can be indexed.  Sounds
like a big mess to me...

[Are there types for which len() returns a value that can't be
indexed?]

--
Grant Edwards                   grante             Yow! It's the RINSE CYCLE!!
                                  at               They've ALL IGNORED the
                               visi.com            RINSE CYCLE!!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rhodri James  
View profile  
 More options Jul 24 2009, 10:11 am
Newsgroups: comp.lang.python
From: "Rhodri James" <rho...@wildebst.demon.co.uk>
Date: Fri, 24 Jul 2009 15:11:40 +0100
Local: Fri, Jul 24 2009 10:11 am
Subject: Re: len() should always return something

Dictionaries.

Which doesn't make your point less valid.  In fact I'd go so
far as to argue that what len() gives you is the number of
items in a container, so len(7) should return 0.

--
Rhodri James *-* Wildebeest Herder to the Masses


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Dickinson  
View profile  
 More options Jul 24 2009, 10:45 am
Newsgroups: comp.lang.python
From: Mark Dickinson <dicki...@gmail.com>
Date: Fri, 24 Jul 2009 07:45:40 -0700 (PDT)
Local: Fri, Jul 24 2009 10:45 am
Subject: Re: len() should always return something
On Jul 24, 3:11 pm, "Rhodri James" <rho...@wildebst.demon.co.uk>
wrote:

> Which doesn't make your point less valid.  In fact I'd go so
> far as to argue that what len() gives you is the number of
> items in a container, so len(7) should return 0.

Nah.  7 contains three bits, so len(7) should *clearly* return 3.

Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
superpollo  
View profile  
 More options Jul 24 2009, 10:50 am
Newsgroups: comp.lang.python
From: superpollo <u...@example.net>
Date: Fri, 24 Jul 2009 16:50:03 +0200
Local: Fri, Jul 24 2009 10:50 am
Subject: Re: len() should always return something

Mark Dickinson wrote:
> On Jul 24, 3:11 pm, "Rhodri James" <rho...@wildebst.demon.co.uk>
> wrote:

>>Which doesn't make your point less valid.  In fact I'd go so
>>far as to argue that what len() gives you is the number of
>>items in a container, so len(7) should return 0.

> Nah.  7 contains three bits, so len(7) should *clearly* return 3.

and len("7") must return 8, by the same token... but wait!

 >>> len("7")
1
 >>>

my python installation must me outdated ;-)

bye


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Jul 24 2009, 10:51 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Fri, 24 Jul 2009 16:51:12 +0200
Local: Fri, Jul 24 2009 10:51 am
Subject: Re: len() should always return something
Mark Dickinson schrieb:

> On Jul 24, 3:11 pm, "Rhodri James" <rho...@wildebst.demon.co.uk>
> wrote:
>> Which doesn't make your point less valid.  In fact I'd go so
>> far as to argue that what len() gives you is the number of
>> items in a container, so len(7) should return 0.

> Nah.  7 contains three bits, so len(7) should *clearly* return 3.

But it contains a minimum of 32 bits! And why are you treating ones as
special over zeros? I thought the times of BitRacism were finally over...

Diez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile  
 More options Jul 24 2009, 11:10 am
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@cs.uu.nl>
Date: Fri, 24 Jul 2009 17:10:07 +0200
Local: Fri, Jul 24 2009 11:10 am
Subject: Re: len() should always return something

But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the
implementation). Therefore it doesn't make much sense.
--
Piet van Oostrum <p...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rhodri James  
View profile  
 More options Jul 24 2009, 1:52 pm
Newsgroups: comp.lang.python
From: "Rhodri James" <rho...@wildebst.demon.co.uk>
Date: Fri, 24 Jul 2009 18:52:41 +0100
Local: Fri, Jul 24 2009 1:52 pm
Subject: Re: len() should always return something
On Fri, 24 Jul 2009 16:10:07 +0100, Piet van Oostrum <p...@cs.uu.nl> wrote:

Quite.

--
Rhodri James *-* Wildebeest Herder to the Masses


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Jul 24 2009, 2:03 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Fri, 24 Jul 2009 11:03:59 -0700
Local: Fri, Jul 24 2009 2:03 pm
Subject: Re: Re: len() should always return something

You could use Python's extended call syntax when defining your function:

def average(*args):
    return sum(args) / len(args)

average(7) #==> 7
average(2,3,4,5,6) #==> 4
average(*[2,3,4,5,6]) #==> 4
average([2,3,4,5,6]) #==> error

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Jul 24 2009, 2:52 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Fri, 24 Jul 2009 14:52:38 -0400
Local: Fri, Jul 24 2009 2:52 pm
Subject: Re: len() should always return something

Phillip M. Feldman wrote:
> I've been converting Matlab codes to Python.  In Matlab, a scalar is
> just a one-by-one matrix and has a length of 1.  This convention seems
> no less arbitrary to me than Python's convention that the concept of
> length is not applicable to ints and floats.

Multiplication of a vector/matrix by a scalar always defined and
commutative. Multiplication of a vector/matrix by a 1x1 matrix is not
always even defined. So not having scalars in a matrix package strikes
me as a bit odd.

 > My workaround was to write

> the following function:

> def is_scalar(x):
>    """Return True if x is an instance of int, float, or complex.
>    Otherwise, return False.  Note: If x is a length-1 list or array
>    containing an int, float, or complex value, False is returned."""
>    if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex):

Better:    if isinstance(x, (int, float, complex)):

but you forgot decimals and fractions and any other possible number modules.

In 3.1,
 >>> from numbers import Number
 >>> from decimal import Decimal
 >>> from fractions import Fraction
 >>> for x in 1, 1.0, (1+0j), Decimal(1), Fraction(1,1):
        isinstance(x, Number)

True
True
True
True
True

and the same for any other module that registers a class as a Number

>       return True
>    return False

> The application is the following: In various types of scientific
> applications, one operates on a list of measurements.  If there is only
> a single measurement, it is reasonable to allow the calling program to
> pass a scalar without wrapping it up into a list or array.

If you want to do that, start with

def f(x):
   try: len(x)
   except TypeError: x = x,

or in 3.1 use Number test above.

Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Jul 24 2009, 2:59 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 24 Jul 2009 18:59:40 GMT
Local: Fri, Jul 24 2009 2:59 pm
Subject: Re: len() should always return something

On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote:
>> Nah.  7 contains three bits, so len(7) should *clearly* return 3.

> and len("7") must return 8, by the same token... but wait!

>  >>> len("7")
> 1

> my python installation must me outdated ;-)

No no no, you're obviously using an awesome version of Python that can
compress single-character strings to a single bit!

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "list vs. tuple [Re: len() should always return something]" by Roy Smith
Roy Smith  
View profile  
 More options Jul 24 2009, 3:03 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Fri, 24 Jul 2009 15:03:29 -0400
Local: Fri, Jul 24 2009 3:03 pm
Subject: list vs. tuple [Re: len() should always return something]
In article <mailman.3674.1248461573.8015.python-l...@python.org>,
 Terry Reedy <tjre...@udel.edu> wrote:

> Better:    if isinstance(x, (int, float, complex)):

I never noticed this before, but it seems odd that the second argument to
isinstance() should be a tuple.  Using the normal arguments made about
tuples vs. lists, it seems like a list would be the right data structure
here.  I suppose a set would be even more right, but (I'm pretty sure)
isinstance() predates sets.

I'm curious why a tuple was chosen.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "len() should always return something" by Roy Smith
Roy Smith  
View profile  
 More options Jul 24 2009, 3:04 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Fri, 24 Jul 2009 15:04:55 -0400
Local: Fri, Jul 24 2009 3:04 pm
Subject: Re: len() should always return something
In article <0279f596$0$5185$c3e8...@news.astraweb.com>,
 Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au> wrote:

> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote:

> >> Nah.  7 contains three bits, so len(7) should *clearly* return 3.

> > and len("7") must return 8, by the same token... but wait!

> >  >>> len("7")
> > 1

> > my python installation must me outdated ;-)

> No no no, you're obviously using an awesome version of Python that can
> compress single-character strings to a single bit!

Compressing strings to a single bit is easy.  It's the uncompressing that's
tricky.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Jul 24 2009, 3:05 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 24 Jul 2009 19:05:11 GMT
Local: Fri, Jul 24 2009 3:05 pm
Subject: Re: len() should always return something

Chris, I'm curious why you think that these Zen are relevant to the OP's
complaint.

Re explicit vs implicit, len(42) is just as explicit as len([42, 23]).

Arguably (I wouldn't argue this, but some people might) ints aren't
"special enough" to break the rule that len(obj) should always return
something.

(I don't actually agree, but some people might be able to produce a
coherent argument why len() should apply equally to all objects.)

Re errors passing silently, the OP doesn't believe that len(42) should be
an error, so that's not relevant.

And there's nothing ambiguous about len(42).

I agree with the current Python behaviour, but I don't think there's
anything in the Zen to support it. As far as I know, there is no
programming language which treats scalars like ints as if they were
vectors of length 1, which makes Python's choice to make ints unlengthed
a no-brainer.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "list vs. tuple [Re: len() should always return something]" by Steven D&#39;Aprano
Steven D'Aprano  
View profile  
 More options Jul 24 2009, 3:18 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 24 Jul 2009 19:18:09 GMT
Local: Fri, Jul 24 2009 3:18 pm
Subject: Re: list vs. tuple [Re: len() should always return something]

On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote:
> In article <mailman.3674.1248461573.8015.python-l...@python.org>,
>  Terry Reedy <tjre...@udel.edu> wrote:

>> Better:    if isinstance(x, (int, float, complex)):

> I never noticed this before, but it seems odd that the second argument
> to isinstance() should be a tuple.  Using the normal arguments made
> about tuples vs. lists, it seems like a list would be the right data
> structure here.

What would be the point of using a list? You're never going to sort it,
or append items to it, or otherwise mutate it. You build it, pass it to a
function which doesn't modify it in any fashion, then it gets garbage
collected.

> I suppose a set would be even more right, but (I'm
> pretty sure) isinstance() predates sets.

Yes.

[steve@sylar ~]$ python1.5
Python 1.5.2 (#1, Apr  1 2009, 22:55:54)  [GCC 4.1.2 20070925 (Red Hat
4.1.2-27)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam

>>> isinstance

<built-in function isinstance>
>>> set

Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: set

> I'm curious why a tuple was chosen.

Tuples are smaller and faster to build than lists -- they're the most
lightweight sequence type in Python. You don't need all the extra
functionality of lists, so why go to the time and effort of building a
list?

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "len() should always return something" by Chris Rebert
Chris Rebert  
View profile  
 More options Jul 24 2009, 4:02 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Fri, 24 Jul 2009 13:02:24 -0700
Local: Fri, Jul 24 2009 4:02 pm
Subject: Re: len() should always return something
On Fri, Jul 24, 2009 at 12:05 PM, Steven

To explain in more detail:

> Re explicit vs implicit, len(42) is just as explicit as len([42, 23]).

If you want a collection (something that has length), then one should
explicitly create one, not implicitly have a singleton value act like
it's a pseudo-collection.
I admit I'm somewhat conflating this principle with the anti-ambiguity
principle, but the two are related, imho.

> Arguably (I wouldn't argue this, but some people might) ints aren't
> "special enough" to break the rule that len(obj) should always return
> something.

Except that's not the current rule. The current rule is that it's
defined only for collections.
One would instead have to argue why ints are special enough to have
len() defined despite not being collections.
I think the point made by Grant Edwards is instructive. len(x) = 1
typically implies list(x)[0] and similar should be valid. Altering the
behavior would invalidate that theorem and cause quite a bit of code
upheaval, all just to save the OP from typing one pair of []s.

> (I don't actually agree, but some people might be able to produce a
> coherent argument why len() should apply equally to all objects.)

Well, yes, this /whole/ "argument" is entirely academic; the behavior
is extremely unlikely to change, we're just trying to give ex post
facto rationales for pedagogical purposes. :)

> Re errors passing silently, the OP doesn't believe that len(42) should be
> an error, so that's not relevant.

True, it would not directly silence an error, but defining len() on
scalars would tend towards obscuring errors in code that incorrectly
treats scalars as collections.

> And there's nothing ambiguous about len(42).

Really? What is its value then? I think arguments of varying quality
can be made for:
1 - as the OP and those from array programming languages would suggest
2 - the number of decimal digits in 42, if one was feeling Perlish
6 - the minimum number of bits necessary to represent 42 in binary
32 (or 64, depending on your CPU) - the number of bits necessary to
represent an int (obviously breaks down a bit with arbitrary-magnitude
ints)
undefined - i.e. it causes an error, the current behavior; asking for
the length of a non-collection is "nonsensical" or "absurd"

> I agree with the current Python behaviour, but I don't think there's
> anything in the Zen to support it. As far as I know, there is no

The problem and strength of the Zen is that it's all about how you
interpret it. :-)

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marcus Wanner  
View profile  
 More options Jul 24 2009, 4:09 pm
Newsgroups: comp.lang.python
From: Marcus Wanner <marc...@cox.net>
Date: Fri, 24 Jul 2009 16:09:15 -0400
Local: Fri, Jul 24 2009 4:09 pm
Subject: Re: len() should always return something
On 7/24/2009 3:04 PM, Roy Smith wrote:

I assume you mean ord("7")%2?

First one to correctly decompress the value 0 into an ASCII character
wins the title of the world's most capable hacker :p

Marcus


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Jul 24 2009, 4:18 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Fri, 24 Jul 2009 21:18:26 +0100
Local: Fri, Jul 24 2009 4:18 pm
Subject: Re: len() should always return something

asciichar = chr(len(0)) if the OP's wishes come true?

--
Kindest regards.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marcus Wanner  
View profile  
 More options Jul 24 2009, 4:25 pm
Newsgroups: comp.lang.python
From: Marcus Wanner <marc...@cox.net>
Date: Fri, 24 Jul 2009 16:25:57 -0400
Local: Fri, Jul 24 2009 4:25 pm
Subject: Re: len() should always return something
On 7/24/2009 4:18 PM, Mark Lawrence wrote:

Nope, wasn't "?"...

Marcus


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Chase  
View profile  
 More options Jul 24 2009, 4:30 pm
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Fri, 24 Jul 2009 15:30:10 -0500
Local: Fri, Jul 24 2009 4:30 pm
Subject: Re: len() should always return something

Marcus Wanner wrote:
> First one to correctly decompress the value 0 into an ASCII
> character wins the title of the world's most capable hacker :p

Bah...uncompressing the value 0 into *an* ASCII character is
easy.  Uncompressing it into the *original* ASCII character from
which it was compressed (with the aforementioned compression
method) becomes a bit trickier ;-)

-tkc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Jul 24 2009, 4:34 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Fri, 24 Jul 2009 13:34:11 -0700
Local: Fri, Jul 24 2009 4:34 pm
Subject: Re: len() should always return something

On Fri, Jul 24, 2009 at 1:30 PM, Tim Chase<python.l...@tim.thechases.com> wrote:
> Marcus Wanner wrote:

>> First one to correctly decompress the value 0 into an ASCII
>> character wins the title of the world's most capable hacker :p

> Bah...uncompressing the value 0 into *an* ASCII character is easy.
>  Uncompressing it into the *original* ASCII character from which it was
> compressed (with the aforementioned compression method) becomes a bit
> trickier ;-)

Deja vu: http://www.hackles.org/cgi-bin/archives.pl?request=310

- Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "list vs. tuple [Re: len() should always return something]" by Terry Reedy
Terry Reedy  
View profile  
 More options Jul 24 2009, 4:43 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Fri, 24 Jul 2009 16:43:17 -0400
Local: Fri, Jul 24 2009 4:43 pm
Subject: Re: list vs. tuple [Re: len() should always return something]

Steven D'Aprano wrote:
> On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote:

>> In article <mailman.3674.1248461573.8015.python-l...@python.org>,
>>  Terry Reedy <tjre...@udel.edu> wrote:

>>> Better:    if isinstance(x, (int, float, complex)):
>> I never noticed this before, but it seems odd that the second argument
>> to isinstance() should be a tuple.  Using the normal arguments made
>> about tuples vs. lists, it seems like a list would be the right data
>> structure here.

I ignore the 'normal arguments' and it seems that Guido or the designer
of isinstance did so here too.  Fortunately.  Practicality beats
'purity', especially misguided purity.

In fact, constant tuples can be and now are compiled as constants:

 >>> dis(compile('(1,2,3)','','eval'))
   1           0 LOAD_CONST               3 ((1, 2, 3))
               3 RETURN_VALUE
 >>> dis(compile('[1,2,3]','','eval'))
   1           0 LOAD_CONST               0 (1)
               3 LOAD_CONST               1 (2)
               6 LOAD_CONST               2 (3)
               9 BUILD_LIST               3
              12 RETURN_VALUE

Internally, even a frozenset is more complicated than a tuple since it
still needs a hash table, which is overkill for something that will be
linearly scanned exactly once.

tjr


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 60   Newer >
« Back to Discussions « Newer topic     Older topic »