Re: TypeError with map with no len()

2017-09-26 Thread john polo
On 9/25/2017 5:37 PM, Thomas Jollans wrote: On 25/09/17 18:44, john polo wrote: Python List, I am trying to make practice data for plotting purposes. I am using Python 3.6. The instructions I have are import matplotlib.pyplot as plt import math import numpy as np t = np.arange(0, 2.5, 0.1) y1

Re: TypeError with map with no len()

2017-09-25 Thread Thomas Jollans
his: t = np.arange(0, 2.5, 0.1) y1 = np.sin(np.pi * t) Without using numpy at all, this might be t = [i * 0.1 for i in range(25)] y1 = [math.pi * a for a in t] > plt.plot(t,y1) > > However, at this point, I get a TypeError that says > > object of type 'map' has no len(

Re: TypeError with map with no len()

2017-09-25 Thread john polo
On 9/25/2017 12:03 PM, Paul Moore wrote: You're using Python 3, and I suspect that you're working from instructions that assume Python 2. In Python 3, the result of map() is a generator, not a list (which is what Python 2's map returned). In order to get an actual list (which appears to be what y

Re: TypeError with map with no len()

2017-09-25 Thread Paul Moore
, math.pi*t) > plt.plot(t,y1) > > However, at this point, I get a TypeError that says > > object of type 'map' has no len() > > In [6]: t > Out[6]: > array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , > 1.1, 1.2, 1.3, 1.4, 1.5,

Re: TypeError with map with no len()

2017-09-25 Thread Grant Edwards
; y1 = map(math.sin, math.pi*t) > plt.plot(t,y1) > > However, at this point, I get a TypeError that says > > object of type 'map' has no len() you probably need to convert y1 from a 'map' object (a type of an iterator) to an array. y1 = map(ma

Re: TypeError with map with no len()

2017-09-25 Thread Peter Otten
in, math.pi*t) > plt.plot(t,y1) > > However, at this point, I get a TypeError that says > > object of type 'map' has no len() > > In [6]: t > Out[6]: > array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , > 1.1, 1.2, 1.3, 1.4, 1.5

Re: TypeError with map with no len()

2017-09-25 Thread Ned Batchelder
) However, at this point, I get a TypeError that says object of type 'map' has no len() In [6]: t Out[6]: array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7, 0.8,  0.9, 1. ,     1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8, 1.9,  2. , 2.1,     2.2,  2.3,  2.4]) In [7]: y1 O

Re: TypeError with map with no len()

2017-09-25 Thread Terry Reedy
y1 = list(map(math.sin, math.pi*t)) or y1 = tuple(map(math.sin, math.pi*t)) (Does not make much difference.) plt.plot(t,y1) or plt.plot(t, list(y1)) However, at this point, I get a TypeError that says object of type 'map' has no len() In 3.x, map returns an iterator. plot needs

TypeError with map with no len()

2017-09-25 Thread john polo
TypeError that says object of type 'map' has no len() In [6]: t Out[6]: array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7, 0.8,  0.9,  1. ,     1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8, 1.9,  2. ,  2.1,     2.2,  2.3,  2.4]) In [7]: y1 Out[7]: In [8]: math.pi*t Out[8]:

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-04 Thread Chris Angelico
On Mon, Dec 5, 2016 at 6:14 AM, Terry Reedy wrote: > On 12/3/2016 7:31 PM, Chris Angelico wrote: >> >> On Sun, Dec 4, 2016 at 11:10 AM, Terry Reedy wrote: But the expression result isn't even used. So this is better written: >>> >>> >>> matplotlib.pyplot.xlabel sets x-axis scaling, with

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-04 Thread Terry Reedy
On 12/3/2016 7:31 PM, Chris Angelico wrote: On Sun, Dec 4, 2016 at 11:10 AM, Terry Reedy wrote: But the expression result isn't even used. So this is better written: matplotlib.pyplot.xlabel sets x-axis scaling, with no documented return value. http://matplotlib.org/api/pyplot_api.html#matplo

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Chris Angelico
On Sun, Dec 4, 2016 at 11:10 AM, Terry Reedy wrote: >> But the expression result isn't even used. So this is better written: > > > matplotlib.pyplot.xlabel sets x-axis scaling, with no documented return > value. > http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel > If, as seems re

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Terry Reedy
if k in [0, len(n_trials) - 1] else None The backslash means "this continues on the next line". > The ternary conditional looks like this: 5 if 1 < 2 else 7 Since 1 < 2, this has the value of 5. If not, it would have the value 7. But the expression result isn'

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread MRAB
On 2016-12-03 23:11, Robert wrote: On Saturday, December 3, 2016 at 6:09:02 PM UTC-5, Robert wrote: Hi, I am trying to understand the meaning of the below code snippet. Though I have a Python IDLE at computer, I can't get a way to know below line: if k in [0, len(n_trials) - 1] else No

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Chris Angelico
On Sun, Dec 4, 2016 at 10:11 AM, Robert wrote: > I just notice that there is a slash character (\) before the if line. > What is it for? Yes, that's important. The entire line of code is: plt.xlabel("$p$, probability of heads") \ if k in [0, len(n_trials

Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Robert
On Saturday, December 3, 2016 at 6:09:02 PM UTC-5, Robert wrote: > Hi, > > I am trying to understand the meaning of the below code snippet. Though I have > a Python IDLE at computer, I can't get a way to know below line: > > if k in [0, len(n_trials) - 1] else None >

What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Robert
Hi, I am trying to understand the meaning of the below code snippet. Though I have a Python IDLE at computer, I can't get a way to know below line: if k in [0, len(n_trials) - 1] else None I feel it is strange for what returns when the 'if' condition is true? The second part &#x

Re: len() of unsized object - ks test

2014-04-25 Thread Jamie Mitchell
,'norm') > > > > # I then get the following error: > > > > File "", line 1, in > > File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", > line 3413, in kstest > > N = len(vals) > > TypeEr

Re: len() of unsized object - ks test

2014-04-25 Thread Steven D'Aprano
t;", line 1, in > File > "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", > line 3413, in kstest > N = len(vals) > TypeError: len() of unsized object > > Any ideas on why this isn't working would be great. My guess is

Re: len() of unsized object - ks test

2014-04-25 Thread MRAB
t;, line 1, in File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", line 3413, in kstest N = len(vals) TypeError: len() of unsized object Any ideas on why this isn't working would be great. -- https://mail.python.org/mailman/listinfo/python-list

len() of unsized object - ks test

2014-04-25 Thread Jamie Mitchell
r') # The string is simply a 1D array # Then performing the ks test: kstest(control,'norm') # I then get the following error: File "", line 1, in File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", line 3413, in kstest N = len(vals) Ty

Re: memoryview (was "len() on mutables vs. immutables")

2013-02-08 Thread Demian Brecht
ndarrays work. Essentially >len and iter treat the array as if it were a list of lists (of lists >...). -- http://mail.python.org/mailman/listinfo/python-list

Re: memoryview (was "len() on mutables vs. immutables")

2013-02-08 Thread Oscar Benjamin
On 8 February 2013 06:24, Demian Brecht wrote: > On 2013-02-07 8:30 PM, "Terry Reedy" wrote: > > If a memoryview (3+) is representing a non-continuguous block of memory (> > 1 > ndim), will len(obj) not return incorrect results? It seems to be > reporting the sha

memoryview (was "len() on mutables vs. immutables")

2013-02-07 Thread Demian Brecht
figured I'd write something about my findings as I dug around the source in order to answer the question for myself. Still wondering about memoryview though: If a memoryview (3+) is representing a non-continuguous block of memory (> 1 ndim), will len(obj) not return incorrect results? It

Re: len() on mutables vs. immutables

2013-02-07 Thread Terry Reedy
On 2/7/2013 8:09 PM, Demian Brecht wrote: http://demianbrecht.github.com/posts/2013/02/07/understanding-len/ When len() is called passing an immutable built-in type (such as a string), I'd assume that the overhead in doing so is simply a function call and there are no on-call calcula

Re: len() on mutables vs. immutables

2013-02-07 Thread Demian Brecht
tions. http://demianbrecht.github.com/posts/2013/02/07/understanding-len/ However, my research brought up a question (I'm assuming someone here can answer this): If a memoryview is representing a non-continuguous block of memory (> 1 ndim), will len(obj) not return incorrect results? I

Re: len() on mutables vs. immutables

2012-10-18 Thread Terry Reedy
On 10/18/2012 2:42 PM, Demian Brecht wrote: Awesome. Pretty much what I figured. Of course, I'll have to dig around the source just to confirm this with my own eyes (more just curiosity than anything), If you do, please followup with a report. -- Terry Jan Reedy -- http://mail.python.org/mai

Re: len() on mutables vs. immutables

2012-10-18 Thread Terry Reedy
On 10/18/2012 3:18 PM, Prasad, Ramit wrote: Terry Reedy wrote: On 10/18/2012 1:23 PM, Demian Brecht wrote: When len() is called passing an immutable built-in type (such as a string), I'd assume that the overhead in doing so is simply a function call and there are no on-call calculations

Re: len() on mutables vs. immutables

2012-10-18 Thread Demian Brecht
On Thu, Oct 18, 2012 at 12:43 PM, Daniel Urban wrote: > The source is usually in Objects/*object.c (e.g., the source for list > is in Objects/listobject.c, dict is in dictobject.c and so on). The > implementation of __len__ is usually in a method called > whatever_length (e.g., dict.__len__ is cal

RE: len() on mutables vs. immutables

2012-10-18 Thread Prasad, Ramit
Ian Kelly wrote: > Sent: Thursday, October 18, 2012 2:39 PM > To: Python > Subject: Re: len() on mutables vs. immutables > > On Thu, Oct 18, 2012 at 1:18 PM, Prasad, Ramit > wrote: > > Why does pointer arithmetic work for dicts? I would think the position > > of

Re: len() on mutables vs. immutables

2012-10-18 Thread Daniel Urban
On Thu, Oct 18, 2012 at 8:42 PM, Demian Brecht wrote: >> str, bytes, bytearrays, arrays, sets, frozensets, dicts, dictviews, and >> ranges should all return len in O(1) time. That includes the possibility >> of a subtraction as indicated above. > > Awesome. Pretty much w

Re: len() on mutables vs. immutables

2012-10-18 Thread Ian Kelly
On Thu, Oct 18, 2012 at 1:18 PM, Prasad, Ramit wrote: > Why does pointer arithmetic work for dicts? I would think the position > of a value would be based on the hash of the key and thus "random" for > the context of this conversation. It doesn't. len() on CPython dicts

RE: len() on mutables vs. immutables

2012-10-18 Thread Prasad, Ramit
Terry Reedy wrote: > On 10/18/2012 1:23 PM, Demian Brecht wrote: > > > When len() is called passing an immutable built-in type (such as a > > string), I'd assume that the overhead in doing so is simply a function > > call and there are no on-call calculations done. Is

RE: len() on mutables vs. immutables

2012-10-18 Thread Nick Cash
into Python (well, > CPython at this point anyway). > > When len() is called passing an immutable built-in type (such as a > string), I'd assume that the overhead in doing so is simply a function > call and there are no on-call calculations done. Is that correct? > > I

Re: len() on mutables vs. immutables

2012-10-18 Thread Demian Brecht
n most dynamic cases (obviously totally depends on implementation details). > str, bytes, bytearrays, arrays, sets, frozensets, dicts, dictviews, and > ranges should all return len in O(1) time. That includes the possibility > of a subtraction as indicated above. Awesome. Pretty much wha

Re: len() on mutables vs. immutables

2012-10-18 Thread Demian Brecht
On 10/18/2012 11:28 AM, Nick Cash wrote: It appears that list has len() complexity of O(1) source: http://wiki.python.org/moin/TimeComplexity It may be worth mentioning that lists in Python are implemented using arrays instead of linked lists. It's reasonable to assume that other bui

Re: len() on mutables vs. immutables

2012-10-18 Thread Terry Reedy
On 10/18/2012 1:23 PM, Demian Brecht wrote: When len() is called passing an immutable built-in type (such as a string), I'd assume that the overhead in doing so is simply a function call and there are no on-call calculations done. Is that correct? See below. I'd also assume th

len() on mutables vs. immutables

2012-10-18 Thread Demian Brecht
l, CPython at this point anyway). When len() is called passing an immutable built-in type (such as a string), I'd assume that the overhead in doing so is simply a function call and there are no on-call calculations done. Is that correct? I'd also assume that mutable built-in types

Re: __getattribute__ hook and len() problem

2010-07-15 Thread ernest
Thanks Chris & Christian. Mistery solved :) Ernest -- http://mail.python.org/mailman/listinfo/python-list

Re: __getattribute__ hook and len() problem

2010-07-15 Thread Christian Heimes
> And yet, p.__len__() returns 3. I though len(object) simply > called object.__len__. Not exactly, all __magic__ methods of new style classes are called like getattr(type(obj), "__len__")(obj). As a result magic methods are never looked up on the object, including hooks like

Re: __getattribute__ hook and len() problem

2010-07-15 Thread Chris Rebert
 else: >            return str(self).__getattribute__(name) >    def write(self, data): >        self.content.append(data) > > Then I do: > > In [50]: p = Part() > > In [51]: p.write('foo') > > However, len(p) fails: > > TypeError: object of type 'P

__getattribute__ hook and len() problem

2010-07-15 Thread ernest
ta) Then I do: In [50]: p = Part() In [51]: p.write('foo') In [52]: p.upper() Out[56]: 'FOO' This is okay, works as expected. However, len(p) fails: TypeError: object of type 'Part' has no len() And yet, p.__len__() returns 3. I though len(object) simply called o

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Stef Mientki
; Now I'm often what to do something if I've more than 1 element in the result. >> So I test: >> >>if len ( Result ) > 1 : >> >> But to prevent exceptions, i've to write ( I often forget) >> if Result and ( len ( Result ) > 1 ) : >&

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Steven D'Aprano
Please pardon me for breaking threading, but Stef's original post has not come through to me. On 6/30/10 11:39 AM, Stef Mientki wrote: > hello, > > I've lot of functions that returns their result in some kind of tuple / > list / array, > and if there is no result, these functions return None.

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Emile van Sebille
I test: which works fine if beforehand you do Result = presumedFuncCall() or [] particularly if you want to test len subsequently. Emile if len ( Result )> 1 : But to prevent exceptions, i've to write ( I often forget) if Result and ( len ( Result )> 1 ) : So I wonder

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Ian Kelly
On Wed, Jun 30, 2010 at 12:39 PM, Stef Mientki wrote: > So I wonder why len is not allowed on None > and if there are objections to extend the len function . For the same reason that (None + 42) doesn't return 42, and that (None.upper()) doesn't return NONE. -- http://mail.py

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Dave Angel
the result. So I test: if len ( Result ) > 1 : But to prevent exceptions, i've to write ( I often forget) if Result and ( len ( Result ) > 1 ) : Just do: if Result: You don't have to do a length check > 1; because if Result has a length of 0, it'll be false too. S

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Stephen Hansen
On 6/30/10 12:02 PM, Tim Chase wrote: On 06/30/2010 01:50 PM, Stephen Hansen wrote: On 6/30/10 11:39 AM, Stef Mientki wrote: if len ( Result )> 1 : But to prevent exceptions, i've to write ( I often forget) if Result and ( len ( Result )> 1 ) : Just do: if Result: You don

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Tim Chase
On 06/30/2010 01:50 PM, Stephen Hansen wrote: On 6/30/10 11:39 AM, Stef Mientki wrote: if len ( Result )> 1 : But to prevent exceptions, i've to write ( I often forget) if Result and ( len ( Result )> 1 ) : Just do: if Result: You don't have to do a length check

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Stefan Behnel
Stef Mientki, 30.06.2010 20:39: I've lot of functions that returns their result in some kind of tuple / list / array, and if there is no result, these functions return None. Now I'm often what to do something if I've more than 1 element in the result. So I test: if len

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Gary Herron
I test: if len ( Result ) > 1 : But to prevent exceptions, i've to write ( I often forget) if Result and ( len ( Result ) > 1 ) : So I wonder why len is not allowed on None and if there are objections to extend the len function . thanks, Stef Mientki Because the natural interp

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Zubin Mithra
re than 1 element in the > result. > So I test: > >if len ( Result ) > 1 : > > But to prevent exceptions, i've to write ( I often forget) > if Result and ( len ( Result ) > 1 ) : > use if Result: Checking the length would be a bad idea. -- http://mail.python.org/mailman/listinfo/python-list

Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Stephen Hansen
On 6/30/10 11:39 AM, Stef Mientki wrote: hello, I've lot of functions that returns their result in some kind of tuple / list / array, and if there is no result, these functions return None. Now I'm often what to do something if I've more than 1 element in the result. So

Hwy doesn't len(None) return zero ?

2010-06-30 Thread Stef Mientki
hello, I've lot of functions that returns their result in some kind of tuple / list / array, and if there is no result, these functions return None. Now I'm often what to do something if I've more than 1 element in the result. So I test: if len ( Result ) > 1 : But to

Re: len() should always return something

2009-07-28 Thread Luis Zarrabeitia
e, btw, will demote a two-dimensional 1x1 matrix to a scalar for Matrix multiplication, which may hide errors) If you are converting from matlab, I'd say you have biggest things to worry about. As you said, you can just replace the len function (even safer may be to do [1]). Assignment, fo

Re: len() should always return something

2009-07-27 Thread Joel Koltner
"Dr. Phillip M. Feldman" wrote in message news:mailman.3699.1248490256.8015.python-l...@python.org... > Here's a simple-minded example: ... > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: Wow, you could substitute "Matlab

Re: len() should always return something

2009-07-27 Thread Grant Edwards
On 2009-07-27, Joshua Kugler wrote: > Dr. Phillip M. Feldman wrote: > >> "As far as I know, there is no programming language which >> treats scalars like ints as if they were vectors of length 1" >> >> Actually, Matlab does: >> length(5) >> ans = >> 1 > > Oddly enough, so does Pe

Re: len() should always return something

2009-07-27 Thread Joshua Kugler
Dr. Phillip M. Feldman wrote: > > "As far as I know, there is no programming language which treats scalars > like ints as if they were > vectors of length 1" > > Actually, Matlab does: > >>> length(5) > ans = > 1 >>> Oddly enough, so does Perl: $ print length(44) 2 (that's in the Zoidb

Re: len() should always return something

2009-07-26 Thread drednot57
To the best of my recollection, the len() function only applies to container objects; i. e. tuples, lists, strings, etc. an integer object is not a container, thus one receives an error when sending an int as an argument for len(). -- http://mail.python.org/mailman/listinfo/python-list

Re: len() should always return something

2009-07-26 Thread Erik Max Francis
Steven D'Aprano wrote: I'm curious what those applications are, because regular multiplication behaves differently depending on whether you have a 1x1 matrix or a scalar: [[2]]*[[1, 2, 3], [2, 3, 4]] is not defined 2*[[1, 2, 3], [2, 3, 4]] = [[2, 4, 6], [2, 6, 8]] I'm curious as to what thes

Re: len() should always return something

2009-07-26 Thread Steven D'Aprano
On Sat, 25 Jul 2009 16:21:39 -0700, Erik Max Francis wrote: > Steven D'Aprano wrote: >> But it's not "practically every function". It's hardly any function at >> all -- in my code, I don't think I've ever wanted this behavior. I >> would consider it an error for function(42) and function([42]) to

Re: len() should always return something

2009-07-26 Thread Emmanuel Surleau
; the expectation of the coder. I say "reasonably", because if > >> you allow SD> unreasonable situations, everything is "ambiguous": > > > > That's for me the reason that len(42) is ambiguous. The OP apparently > > had 1 as expectation, whereas m

Re: len() should always return something

2009-07-25 Thread Chris Rebert
On Sat, Jul 25, 2009 at 6:47 PM, Erik Max Francis wrote: > Chris Rebert wrote: >> >> On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: >>> >>> Steven D'Aprano wrote: But it's not "practically every function". It's hardly any function at all -- in my code, I don't think I'

Re: len() should always return something

2009-07-25 Thread Erik Max Francis
Chris Rebert wrote: On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: Steven D'Aprano wrote: But it's not "practically every function". It's hardly any function at all -- in my code, I don't think I've ever wanted this behavior. I would consider it an error for function(42) and function(

Re: len() should always return something

2009-07-25 Thread Chris Rebert
On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: > Steven D'Aprano wrote: >> >> But it's not "practically every function". It's hardly any function at all >> -- in my code, I don't think I've ever wanted this behavior. I would >> consider it an error for function(42) and function([42]) to b

Re: len() should always return something

2009-07-25 Thread Erik Max Francis
Steven D'Aprano wrote: But it's not "practically every function". It's hardly any function at all -- in my code, I don't think I've ever wanted this behavior. I would consider it an error for function(42) and function([42]) to behave the same way. One is a scalar, and the other is a vector -- t

Re: len() should always return something

2009-07-25 Thread Marcus Wanner
is "ambiguous": That's for me the reason that len(42) is ambiguous. The OP apparently had 1 as expectation, whereas my first thought was the minimal number of bits to represent the number and 7.5 million came later :=). The number of bits I certainly find reasonable, and I would fi

Re: len() should always return something

2009-07-25 Thread Robert Kern
): if isinstance(xs,(int,float,complex)): xs= [xs] for x in xs: print x Having to put such extra logic into practically every function is one of the annoying things about Python. And where comes "len(xs)" into play here? What you want is iteration over scalars. He explained in another post

Re: len() should always return something

2009-07-25 Thread Robert Kern
On 2009-07-24 21:50, Dr. Phillip M. Feldman wrote: Here's a simple-minded example: def dumbfunc(xs): for x in xs: print x This function works fine if xs is a list of floats, but not if it is single float. It can be made to work as follows: def dumbfunc(xs): if isinstance(xs,(in

Re: len() should always return something

2009-07-25 Thread Rhodri James
On Sat, 25 Jul 2009 03:50:54 +0100, Dr. Phillip M. Feldman wrote: Here's a simple-minded example: def dumbfunc(xs): for x in xs: print x This function works fine if xs is a list of floats, but not if it is single float. It can be made to work as follows: def dumbfunc(xs):

Re: len() should always return something

2009-07-25 Thread Hendrik van Rooyen
On Saturday 25 July 2009 14:59:43 Steven D'Aprano wrote: > On Sat, 25 Jul 2009 10:03:58 +0200, Piet van Oostrum wrote: > >>S> And there's nothing ambiguous about len(42). > > > > len(42) should be 7.5 million. > > And "I don't understand you

Re: len() should always return something

2009-07-25 Thread Piet van Oostrum
>>>>> Steven D'Aprano (SD) wrote: >SD> Ambiguity essentially boils down to being unable to reasonably predict >SD> the expectation of the coder. I say "reasonably", because if you allow >SD> unreasonable situations, everything is "ambiguous&

Re: len() should always return something

2009-07-25 Thread Steven D'Aprano
On Sat, 25 Jul 2009 10:03:58 +0200, Piet van Oostrum wrote: >>S> And there's nothing ambiguous about len(42). > > len(42) should be 7.5 million. And "I don't understand your reasoning".upper() should be "Millennium Hand and Shrimp!". Every

Re: len() should always return something

2009-07-25 Thread Marcus Wanner
On 7/25/2009 5:34 AM, Hendrik van Rooyen wrote: On Friday 24 July 2009 22:09:15 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 that is easy. the xor of 0 and 1 is 1, which is ASCII soh, if I reme

Re: len() should always return something

2009-07-25 Thread Hendrik van Rooyen
On Friday 24 July 2009 22:09:15 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 that is easy. the xor of 0 and 1 is 1, which is ASCII soh, if I remember right. soh is start of header. Burroughs

Re: len() should always return something

2009-07-25 Thread Hendrik van Rooyen
On Friday 24 July 2009 21:04:55 Roy Smith wrote: > Compressing strings to a single bit is easy. It's the uncompressing that's > tricky. Not really - all you have to do is to apply the EXACT same sequence of operations that compressed it, in reverse. The unfortunate part is that this informatio

Re: len() should always return something

2009-07-25 Thread Piet van Oostrum
>>>>> Steven D'Aprano (S) wrote: >S> Chris, I'm curious why you think that these Zen are relevant to the OP's >S> complaint. >S> Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). >S> Arguably (I wouldn't argue th

Re: len() should always return something

2009-07-25 Thread Diez B. Roggisch
): xs= [xs] for x in xs: print x Having to put such extra logic into practically every function is one of the annoying things about Python. And where comes "len(xs)" into play here? What you want is iteration over scalars. I do think that if you frequently have to write code

Re: len() should always return something

2009-07-25 Thread Carl Banks
On Jul 23, 11:35 pm, "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 so

Re: len() should always return something

2009-07-25 Thread Hendrik van Rooyen
On Friday 24 July 2009 16:45:40 Mark Dickinson wrote: > On Jul 24, 3:11 pm, "Rhodri James" > > 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 co

Re: len() should always return something

2009-07-25 Thread Steven D'Aprano
u can now easily get the behaviour you want with a single extra line per function: >>> @matlab ... def mean(numbers): ... return sum(numbers)/len(numbers) ... >>> mean([4.5]) 4.5 >>> mean(4.5) 4.5 >>> mean([4.5, 3.6]) 4.0498 Decorators are extr

Re: len() should always return something

2009-07-24 Thread Andre Engels
On Sat, Jul 25, 2009 at 4:50 AM, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): >   for x in xs: >      print x > > This function works fine if xs is a list of floats, but not if it is single > float.  It can be made to work as follows: > > def dumbfunc(xs):

Re: len() should always return something

2009-07-24 Thread Carl Banks
On Jul 24, 6:57 am, Grant Edwards wrote: > On 2009-07-24, 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 er

Re: len() should always return something

2009-07-24 Thread Chris Rebert
On Fri, Jul 24, 2009 at 7:50 PM, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): >   for x in xs: >      print x > > This function works fine if xs is a list of floats, but not if it is single > float.  It can be made to work as follows: > > def dumbfunc(xs):

Re: len() should always return something

2009-07-24 Thread Dr. Phillip M. Feldman
;> 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

Re: len() should always return something

2009-07-24 Thread Dr. Phillip M. Feldman
"As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1" Actually, Matlab does: >> length(5) ans = 1 >> -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-somethi

Re: list vs. tuple [Re: len() should always return something]

2009-07-24 Thread Dr. Phillip M. Feldman
isinstance(x, (int, float, complex)) is certainly very compact, and does what I want. Thanks! -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654347.html Sent from the Python - python-list mailing list archive at Nabble.com

Re: len() should always return something

2009-07-24 Thread bartc
"Dr. Phillip M. Feldman" wrote in message news:mailman.3644.1248417347.8015.python-l...@python.org... 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 in

Re: list vs. tuple [Re: len() should always return something]

2009-07-24 Thread Duncan Booth
Roy Smith wrote: > In article , > Terry Reedy 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 lik

Re: len() should always return something

2009-07-24 Thread Terry Reedy
Chris Rebert wrote: I think the point made by Grant Edwards is instructive. len(x) = 1 typically implies list(x)[0] and similar should be valid. At least, one should be able to iterate with x and get len(x) items. See below. And there's nothing ambiguous about len(42). Really? Wh

Re: list vs. tuple [Re: len() should always return something]

2009-07-24 Thread Terry Reedy
Steven D'Aprano wrote: On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: In article , Terry Reedy 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 argume

Re: len() should always return something

2009-07-24 Thread Chris Rebert
On Fri, Jul 24, 2009 at 1:30 PM, Tim Chase 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 int

Re: len() should always return something

2009-07-24 Thread Tim Chase
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

Re: len() should always return something

2009-07-24 Thread Marcus Wanner
On 7/24/2009 4:18 PM, Mark Lawrence wrote: Marcus Wanner wrote: On 7/24/2009 3:04 PM, Roy Smith wrote: In article <0279f596$0$5185$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: Nah. 7 contains three bits, so le

Re: len() should always return something

2009-07-24 Thread Mark Lawrence
Marcus Wanner wrote: On 7/24/2009 3:04 PM, Roy Smith wrote: In article <0279f596$0$5185$c3e8...@news.astraweb.com>, Steven D'Aprano 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

Re: len() should always return something

2009-07-24 Thread Marcus Wanner
On 7/24/2009 3:04 PM, Roy Smith wrote: In article <0279f596$0$5185$c3e8...@news.astraweb.com>, Steven D'Aprano 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

Re: len() should always return something

2009-07-24 Thread Chris Rebert
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,

Re: list vs. tuple [Re: len() should always return something]

2009-07-24 Thread Steven D'Aprano
On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: > In article , > Terry Reedy 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 > a

Re: len() should always return something

2009-07-24 Thread Steven D'Aprano
On Fri, 24 Jul 2009 00:02:28 -0700, Chris Rebert wrote: > On Thu, Jul 23, 2009 at 11:35 PM, 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 doe

Re: len() should always return something

2009-07-24 Thread Roy Smith
In article <0279f596$0$5185$c3e8...@news.astraweb.com>, Steven D'Aprano 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,

  1   2   3   >