Re: NoneType List

2022-12-31 Thread dn

On 31/12/2022 18.45, Goran Ikac wrote:

Happy New Year, everybody!
I'm new in the Python List, new in Python world, and new in coding.
A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.
In the meantime, I found a part of the solution, but a part still remains a
mystery for me. Please run this small snippet, and help.
Thanks

a = [1, 2]
print()

print("a ==", a)
print("type(a) is", type(a))

b = a.append(3)

print("b ==", b)
print("type(b) is", type(b))


Please review https://docs.python.org/3/tutorial/datastructures.html

It is not immediately obvious to people who are not used to 
object-oriented programming.


As observed, "a" is a list. The list contains a pair of numeric-values.

The append is a "method". Methods are functions which belong to classes 
(let's say). Accordingly, the operation is performed on that class, ie 
the appending is to the "a" list.


This can be proven by adding to the above:

print( "\na (appended) =", a )
[1, 2, 3]

As far as the expression on the right-hand side of (the equals sign) 
there is no output-value, hence b == None.



For comparison, try:

b = a + [ 4 ]

and understand why the answer is different, and thus why the first 
'problem' behavior works the way it does...



print( 'Happy new year!' )


PS are you aware of the Python-Tutor Discussion List? It may be more 
suited to assisting your learning intentions...

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Alan Gauld
On 31/12/2022 05:45, Goran Ikac wrote:

> b = a.append(3)


> I mean: why b = a.append(something) is the None type, and how to make a new
> list that contains all the items from a and some new items?

append() like many other collection methods in Python works
in place and returns None. But the action has succeeded
and 3 will have been appended to list 'a'.

So, to create a new list that contains all the old items you could do:

newlist = []   # an empty list
for item in oldlist:
newlist.append(item)

This is so common Python has a shorthand form to do this:

newlist = [item for item in oldlist]

called a list comprehension.

And there is an even shorter way using something called slicing:

newlist = oldlist[:]# copy oldlist to new.


However, as an ex-Smalltalk programmer, I do wish that Python
returned self from these methods rather than None so that
we could chain them. But sadly it doesn't.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Chris Roy-Smith

On 31/12/22 16:45, Goran Ikac wrote:

Happy New Year, everybody!
I'm new in the Python List, new in Python world, and new in coding.
A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.
In the meantime, I found a part of the solution, but a part still remains a
mystery for me. Please run this small snippet, and help.
Thanks

a = [1, 2]
print()

print("a ==", a)
print("type(a) is", type(a))

b = a.append(3)

append is not used that way, append is a method, not a function.

have a look at https://www.w3schools.com/python/ref_list_append.asp.

regards, Chris




print("\nb =", "a.append(3)")
print("b ==", b)
print("type(b) is", type(b))

c = ['1', '2']
print("\nc ==", c)
print("type(c) is", type(c))

d = c.append('3')
print("\nd =", "c.append('3')")
print("d ==", d)
print("type(d) is", type(d))

"""
I mean: why b = a.append(something) is the None type, and how to make a new
list
that contains all the items from a and some new items?
I wasn't able to solve it in a few hours :(
Now I know:
"""

crta = '='
print('\n', 4 * ' ', crta * (len('The solution:')), sep='')
print(3 * ' ', 'The solution:')
print(4 * ' ', crta * (len('The solution:')), sep='')
print('\nThe solution is the slice, an element of Python syntax that
allows')
print('to make a brand new copy of a list, or parts of a list.')
print('The slice actually copies the list\'s contents, not the list\'s
name:')

print()
a = [1, 2]
print("a ==", a)
print("type(a) is", type(a))

b = a[:]
print("\nb = a[:]")
print("b ==", b)
b.append(3)
print("\nb =", "b.append(3)")
print("b ==", b)
print("type(b) is", type(b))
print("\na ==", a)

print('But I still don't know why "b = a.append(something)" is the None
type.')
print('Is there anybody out there?!')


--
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Weatherby,Gerard
Just use the addition operator:

a = [1,2]

a = a + [3,4]

a is now [1, 2, 3, 4]

From: Python-list  on 
behalf of Goran Ikac 
Date: Saturday, December 31, 2022 at 1:53 AM
To: python-list@python.org 
Subject: NoneType List
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

Happy New Year, everybody!
I'm new in the Python List, new in Python world, and new in coding.
A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.
In the meantime, I found a part of the solution, but a part still remains a
mystery for me. Please run this small snippet, and help.
Thanks

a = [1, 2]
print()

print("a ==", a)
print("type(a) is", type(a))

b = a.append(3)
print("\nb =", "a.append(3)")
print("b ==", b)
print("type(b) is", type(b))

c = ['1', '2']
print("\nc ==", c)
print("type(c) is", type(c))

d = c.append('3')
print("\nd =", "c.append('3')")
print("d ==", d)
print("type(d) is", type(d))

"""
I mean: why b = a.append(something) is the None type, and how to make a new
list
that contains all the items from a and some new items?
I wasn't able to solve it in a few hours :(
Now I know:
"""

crta = '='
print('\n', 4 * ' ', crta * (len('The solution:')), sep='')
print(3 * ' ', 'The solution:')
print(4 * ' ', crta * (len('The solution:')), sep='')
print('\nThe solution is the slice, an element of Python syntax that
allows')
print('to make a brand new copy of a list, or parts of a list.')
print('The slice actually copies the list\'s contents, not the list\'s
name:')

print()
a = [1, 2]
print("a ==", a)
print("type(a) is", type(a))

b = a[:]
print("\nb = a[:]")
print("b ==", b)
b.append(3)
print("\nb =", "b.append(3)")
print("b ==", b)
print("type(b) is", type(b))
print("\na ==", a)

print('But I still don't know why "b = a.append(something)" is the None
type.')
print('Is there anybody out there?!')
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!mv_VSTDWgnB7T16IHcBh7TaT3whji-SsPeUtaBmHdSsIJknJZ16qeALOSjCkjCmqheE1pJ-47P_mwAauV-0TjQo$
-- 
https://mail.python.org/mailman/listinfo/python-list


NoneType List

2022-12-31 Thread Thomas Passin

Happy New Year, everybody!
I'm new in the Python List, new in Python world, and new in coding.
A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.
In the meantime, I found a part of the solution, but a part still remains a
mystery for me. Please run this small snippet, and help.
Thanks

a = [1, 2]
print()

print("a ==", a)
print("type(a) is", type(a))

b = a.append(3)
print("\nb =", "a.append(3)")
print("b ==", b)
print("type(b) is", type(b))

c = ['1', '2']
print("\nc ==", c)
print("type(c) is", type(c))

d = c.append('3')
print("\nd =", "c.append('3')")
print("d ==", d)
print("type(d) is", type(d))

"""
I mean: why b = a.append(something) is the None type, and how to make a new
list
that contains all the items from a and some new items?
I wasn't able to solve it in a few hours :(
Now I know:
"""

crta = '='
print('\n', 4 * ' ', crta * (len('The solution:')), sep='')
print(3 * ' ', 'The solution:')
print(4 * ' ', crta * (len('The solution:')), sep='')
print('\nThe solution is the slice, an element of Python syntax that
allows')
print('to make a brand new copy of a list, or parts of a list.')
print('The slice actually copies the list\'s contents, not the list\'s
name:')

print()
a = [1, 2]
print("a ==", a)
print("type(a) is", type(a))

b = a[:]
print("\nb = a[:]")
print("b ==", b)
b.append(3)
print("\nb =", "b.append(3)")
print("b ==", b)
print("type(b) is", type(b))
print("\na ==", a)

print('But I still don't know why "b = a.append(something)" is the None
type.')
print('Is there anybody out there?!')
--
https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list


Re: set.add() doesn't replace equal element

2022-12-31 Thread Ian Pilcher

On 12/30/22 17:00, Paul Bryan wrote:
It seems to me like you have to ideas of what "equal" means. You want to 
update a "non-equal/equal" value in the set (because of a different time 
stamp). If you truly considered them equal, the time stamp would be 
irrelevant and updating the value in the set would be unnecessary.


I would:

a) /not/ consider two different leases with two different time stamps to 
be equal, and
b) as already mentioned, store them in another data structure like a 
dictionary.


Not knowing the specifics of the DHCP object structure, if a DHCP lease 
object has some immutable key or other durable immutable attribute, I 
would be inclined to make that the dictionary key, and store the DHCP 
object as the value.


I have come to the conclusion that you are correct.  Thanks!

--

Google  Where SkyNet meets Idiocracy


--
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Thomas Passin

Oops, my reply got lost somehow.  Here it is:

Everyone's answer to date has been too complicated.  What is going on is 
that list.append() changes the list in place.  It returns nothing.  If 
you want to append an item and then assign the result to a new list, you 
have to do just that:


l1.append(item)
# If we want a *copy* of the appended list:
l2 = l1[:]  # Changes to l2 will not change l1

# If we want another name for the appended list:
l2 = l1  # Changes to l2 will change l1 since they are the same object

list.sort() also operates in place.  There is a function sorted() that 
returns the sorted list (without changing the original list).


The same thing is true of set.add(). The set is changed in place, and 
nothing is returned.


On 12/31/2022 10:50 AM, Thomas Passin wrote:

Happy New Year, everybody!
I'm new in the Python List, new in Python world, and new in coding.
A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.
In the meantime, I found a part of the solution, but a part still remains a
mystery for me. Please run this small snippet, and help.
Thanks

a = [1, 2]
print()

print("a ==", a)
print("type(a) is", type(a))

b = a.append(3)
print("\nb =", "a.append(3)")
print("b ==", b)
print("type(b) is", type(b))

c = ['1', '2']
print("\nc ==", c)
print("type(c) is", type(c))

d = c.append('3')
print("\nd =", "c.append('3')")
print("d ==", d)
print("type(d) is", type(d))

"""
I mean: why b = a.append(something) is the None type, and how to make a new
list
that contains all the items from a and some new items?
I wasn't able to solve it in a few hours :(
Now I know:
"""

crta = '='
print('\n', 4 * ' ', crta * (len('The solution:')), sep='')
print(3 * ' ', 'The solution:')
print(4 * ' ', crta * (len('The solution:')), sep='')
print('\nThe solution is the slice, an element of Python syntax that
allows')
print('to make a brand new copy of a list, or parts of a list.')
print('The slice actually copies the list\'s contents, not the list\'s
name:')

print()
a = [1, 2]
print("a ==", a)
print("type(a) is", type(a))

b = a[:]
print("\nb = a[:]")
print("b ==", b)
b.append(3)
print("\nb =", "b.append(3)")
print("b ==", b)
print("type(b) is", type(b))
print("\na ==", a)

print('But I still don't know why "b = a.append(something)" is the None
type.')
print('Is there anybody out there?!')


--
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread MRAB

On 2022-12-31 05:45, Goran Ikac wrote:

Happy New Year, everybody!
I'm new in the Python List, new in Python world, and new in coding.
A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.
In the meantime, I found a part of the solution, but a part still remains a
mystery for me. Please run this small snippet, and help.
Thanks

a = [1, 2]
print()

print("a ==", a)
print("type(a) is", type(a))

b = a.append(3)
print("\nb =", "a.append(3)")
print("b ==", b)
print("type(b) is", type(b))

c = ['1', '2']
print("\nc ==", c)
print("type(c) is", type(c))

d = c.append('3')
print("\nd =", "c.append('3')")
print("d ==", d)
print("type(d) is", type(d))

"""
I mean: why b = a.append(something) is the None type, and how to make a new
list
that contains all the items from a and some new items?
I wasn't able to solve it in a few hours :(
Now I know:
"""

crta = '='
print('\n', 4 * ' ', crta * (len('The solution:')), sep='')
print(3 * ' ', 'The solution:')
print(4 * ' ', crta * (len('The solution:')), sep='')
print('\nThe solution is the slice, an element of Python syntax that
allows')
print('to make a brand new copy of a list, or parts of a list.')
print('The slice actually copies the list\'s contents, not the list\'s
name:')

print()
a = [1, 2]
print("a ==", a)
print("type(a) is", type(a))

b = a[:]
print("\nb = a[:]")
print("b ==", b)
b.append(3)
print("\nb =", "b.append(3)")
print("b ==", b)
print("type(b) is", type(b))
print("\na ==", a)

print('But I still don't know why "b = a.append(something)" is the None
type.')
print('Is there anybody out there?!')


Methods that modify in-place usually return None. "a.append(something)" 
modifies (appends to) the list 'a' and returns None.


If you want to a new line with something at the end try "b = a + 
[something]".


--
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread dn

On 31/12/2022 18.45, Goran Ikac wrote:
...

A few days (weeks?) ago, I faced a problem trying to write a program for an
exercise. I asked for help and nobody answered.


Looking back over the last six months of List-Archives, your name does 
not appear against a single post. This may explain why "nobody answered".


However, ten hours after the above/first message, you posted again. This 
time as "Thomas Passin". That was followed an hour-or-so later, with a 
reply-to-self saying: "Everyone's answer to date has been too 
complicated", and then basically repeating the information 
previously-provided by a number of contributors.


It then goes on to talk about copying, sorting, adding, and even sets. 
Much of which you had earlier said: "I know". Was there a further 
question in there?


Which part of which answer did you find "too complicated"?

Did you try the experiments suggested, and read the references-provided?

Please ask a further question detailing what you have understood, and 
what is still mystifying. Folk here endeavor to be helpful (see also, 
earlier reference to the Python-Tutor list).


When asking a question here, please try to reduce the problem to its 
simplest form, so that you're not asking volunteers to read 
multiple-screens of irrelevant code. It will help to ask one question at 
a time, or to carefully separate multiple questions.


Yes, this can be difficult when one is learning. That said, we all 
started somewhere and are happy to help you to become a valued colleague!


--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


RE: NoneType List

2022-12-31 Thread avi.e.gross
It depends on what people consider too complicated.

I find it a tad complicated when someone posts using two different ID, and
then wonders ...

The question related to taking a list and extending it and using the result
in an assignment statement.

There were several inter-related questions people responded to.

One was asking why something did not work as expected. Several answers
pointed out it was because it was not designed the way expected and reading
the manual page or other reference material would help explain that you need
to use code the way it was intended and not as you wish it. Other aspects of
similar code that do what you expect were shown and you could pick any you
liked.

A second question some were answering is various ways to get the
functionality wanted. Some were simple enough like using "+" and others did
seem complex as they showed many variations on copying an object or how to
make a subclass that effectively substitutes a method that returns the
internally changed object.

And, of course, we had the philosophical question of why the feature was
designed to not return anything (well, NULL) rather than return the changed
object. Returning nothing is arguably slightly more efficient in the many
cases where the return value is simply ignored and thus tossed. But as
mentioned, it would be nice for some purposes, including chaining, to be
able to write something like 

result = lst.add_ret("item").sort_ret()

As mentioned, this can still be easily done using something like:

result = sorted(lst + "item") 

-Original Message-
From: Python-list  On
Behalf Of dn
Sent: Saturday, December 31, 2022 3:59 PM
To: python-list@python.org
Subject: Re: NoneType List

On 31/12/2022 18.45, Goran Ikac wrote:
...
> A few days (weeks?) ago, I faced a problem trying to write a program 
> for an exercise. I asked for help and nobody answered.

Looking back over the last six months of List-Archives, your name does not
appear against a single post. This may explain why "nobody answered".

However, ten hours after the above/first message, you posted again. This
time as "Thomas Passin". That was followed an hour-or-so later, with a
reply-to-self saying: "Everyone's answer to date has been too complicated",
and then basically repeating the information previously-provided by a number
of contributors.

It then goes on to talk about copying, sorting, adding, and even sets. 
Much of which you had earlier said: "I know". Was there a further question
in there?

Which part of which answer did you find "too complicated"?

Did you try the experiments suggested, and read the references-provided?

Please ask a further question detailing what you have understood, and what
is still mystifying. Folk here endeavor to be helpful (see also, earlier
reference to the Python-Tutor list).

When asking a question here, please try to reduce the problem to its
simplest form, so that you're not asking volunteers to read multiple-screens
of irrelevant code. It will help to ask one question at a time, or to
carefully separate multiple questions.

Yes, this can be difficult when one is learning. That said, we all started
somewhere and are happy to help you to become a valued colleague!

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Thomas Passin

On 12/31/2022 3:58 PM, dn wrote:

On 31/12/2022 18.45, Goran Ikac wrote:
...
A few days (weeks?) ago, I faced a problem trying to write a program 
for an

exercise. I asked for help and nobody answered.


Looking back over the last six months of List-Archives, your name does 
not appear against a single post. This may explain why "nobody answered".


However, ten hours after the above/first message, you posted again. This 
time as "Thomas Passin". 


That message was probably a mistaken one from me.  I had composed a 
reply but through some mental glitch had to re-do it.  I managed to send 
it with the only quoted thread but not the reply I had wanted to 
include.  So I added my reply and sent it again.  It was probably 
confusing, and I'm sorry about that.


That was followed an hour-or-so later, with a 
reply-to-self saying: "Everyone's answer to date has been too 
complicated", and then basically repeating the information 
previously-provided by a number of contributors.


It then goes on to talk about copying, sorting, adding, and even sets. 
Much of which you had earlier said: "I know". Was there a further 
question in there?


Which part of which answer did you find "too complicated"?

Did you try the experiments suggested, and read the references-provided?

Please ask a further question detailing what you have understood, and 
what is still mystifying. Folk here endeavor to be helpful (see also, 
earlier reference to the Python-Tutor list).


When asking a question here, please try to reduce the problem to its 
simplest form, so that you're not asking volunteers to read 
multiple-screens of irrelevant code. It will help to ask one question at 
a time, or to carefully separate multiple questions.


Yes, this can be difficult when one is learning. That said, we all 
started somewhere and are happy to help you to become a valued colleague!




--
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Greg Ewing

On 1/01/23 11:36 am, avi.e.gr...@gmail.com wrote:

And, of course, we had the philosophical question of why the feature was
designed to not return anything ... rather than return the changed
object.


My understanding is that Guido designed it that way to keep a
clear separation between mutating and non-mutating methods, and
to help catch mistakes resulting from mixing them up.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


RE: NoneType List

2022-12-31 Thread avi.e.gross
Agreed, there are lots of pro/con arguments and the feature is what it  is
historically and not trivial to change. Inline changes to an object make
sense to just be done "silently" and if there are errors, they propagate the
usual way.

As Guido was a major influence at that time,  one view was seen as more
important and prevailed.

Had a language like that been created today, I wonder if some designs might
have looked a bit different so that some functions could be called with
optional arguments that specified what the user wanted returned.

In particular, besides a function where you add a value returning
nothing/None, there may be room for other choices and any choice hard-wired
in would eleicit complaints from others.

lst.add("Value") 

could also return one of several other things such as a pointer to the
upgraded object but also of  a pointer to the object as it looked before the
change (not a serious proposal) or a True/False value specifying if the
change was able to be completed (such as running out of memory, or the
addition not being something you can put in the object) or even return what
was added or how many objects are now in the object at the top level, or how
many times the method was called so far!

I suspect, at the expense of some overhead, you could just add an argument
to many kinds of methods or even functions such as returning='option' that
guides what you want returned, with the default often being what Guido and
others currently have set.

Python already allows functions to return anything they feel like, so this
probably would not break many things.

Of course there are other paths in that direction, such as setting an
attribute of the list/object first that affects how things get returned but
that seems more cumbersome and makes all kinds of errors more likely. Still,
that is a path often used by some Python modules where objects are created
and then tweaked to behave in various ways when later methods are invoked.

But is any of it needed? The current system generally works fine and we have
seen many ways to get other results without tampering with the current
implementation.

This may be yet another example of people who come to python with
pre-existing bias, such as insisting it work like another language they have
used, or wanting the computer to do what they MEANT rather than what they
explicitly or implicitly programmed!


-Original Message-
From: Python-list  On
Behalf Of Greg Ewing
Sent: Saturday, December 31, 2022 7:21 PM
To: python-list@python.org
Subject: Re: NoneType List

On 1/01/23 11:36 am, avi.e.gr...@gmail.com wrote:
> And, of course, we had the philosophical question of why the feature 
> was designed to not return anything ... rather than return the changed 
> object.

My understanding is that Guido designed it that way to keep a clear
separation between mutating and non-mutating methods, and to help catch
mistakes resulting from mixing them up.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Chris Angelico
On Sun, 1 Jan 2023 at 14:19,  wrote:
> Had a language like that been created today, I wonder if some designs might
> have looked a bit different so that some functions could be called with
> optional arguments that specified what the user wanted returned.

Frankly, I doubt it. While you can argue "returning self is more
useful" vs "returning None is more clear if you get it wrong", having
options does NOT improve things, and just makes everything more
complicated, slower, harder to comprehend, and generally worse to work
with.

A language should have some sort of consistent behaviour and stick to
it. If that's not possible, an object type should at least have that.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: NoneType List

2022-12-31 Thread avi.e.gross
Chris,

There is much to say about consistent behavior as compared to flexibility
and convenience.

 I have seen other languages provide functions for example, where the result
can vary and often cause confusion. R had a function that would sometimes
notice the result could be simplified and return that. Guess what? It caused
lots of problems and an option was added that said it should NOT simplify
and always return the same kind of thing.

Consider what happens if a calculation that returned a matrix would decide
that is there was only one columns, it would return a vector/array/whatever
as a one-dimensional result and if the calculation produced a 1 by 1 matrix,
it would simply return a scalar value!

But it may be a feature you want in some cases, albeit rarely when the
results of the function are fed into something that is not expecting it.
Various forms of polymorphism can be helpful but can also be very confusing.

Some solutions are what I have described in earlier messages and some
languages blur distinctions. Again, in R, there may not be a scalar as all
you have is a vector of length one. But matrices even of 1-D are not vectors
and I have had to interconvert between them to make some code run.

I am not making comparisons in the sense that nothing other languages choose
to do is binding on what Python should do. Still, I think it is wrong to
suggest that it does not often do partially ambiguous things from some
perspective. Many functions will take a variety of arguments and return
something reasonable but different each time. 

As a dumb example, what does a simple function like max() return if fed just
integers, just floating point or a combination of both? It seems to return
whatever type the maximum indicates. But it also accepts characters and
sorts them appropriately returning type 'str' and probably accepts and
returns  all kinds of objects if you specify a key function.

Is that so much different than we are discussing in that there isn't any
absolute consistency and things can go various ways in terms of return
value? Heck, I can even make max() return None!


-Original Message-
From: Python-list  On
Behalf Of Chris Angelico
Sent: Saturday, December 31, 2022 10:23 PM
To: python-list@python.org
Subject: Re: NoneType List

On Sun, 1 Jan 2023 at 14:19,  wrote:
> Had a language like that been created today, I wonder if some designs 
> might have looked a bit different so that some functions could be 
> called with optional arguments that specified what the user wanted
returned.

Frankly, I doubt it. While you can argue "returning self is more useful" vs
"returning None is more clear if you get it wrong", having options does NOT
improve things, and just makes everything more complicated, slower, harder
to comprehend, and generally worse to work with.

A language should have some sort of consistent behaviour and stick to it. If
that's not possible, an object type should at least have that.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: NoneType List

2022-12-31 Thread Chris Angelico
On Sun, 1 Jan 2023 at 15:16,  wrote:
>
> Chris,
>
> There is much to say about consistent behavior as compared to flexibility
> and convenience.
>
>  I have seen other languages provide functions for example, where the result
> can vary and often cause confusion. R had a function that would sometimes
> notice the result could be simplified and return that. Guess what? It caused
> lots of problems and an option was added that said it should NOT simplify
> and always return the same kind of thing.

The option was presumably added because backward compatibility is
important, but it would have been better to not need the option in the
first place.

> As a dumb example, what does a simple function like max() return if fed just
> integers, just floating point or a combination of both? It seems to return
> whatever type the maximum indicates. But it also accepts characters and
> sorts them appropriately returning type 'str' and probably accepts and
> returns  all kinds of objects if you specify a key function.
>
> Is that so much different than we are discussing in that there isn't any
> absolute consistency and things can go various ways in terms of return
> value? Heck, I can even make max() return None!
>

... yes? So? It still always returns the largest item in the list, for
some definition of "largest". You'll never have that value returned
wrapped up in a single-element list, though.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list