Re: Puzzling difference between lists and tuples

2020-09-20 Thread Cameron Simpson
On 20Sep2020 20:33, Avi Gross  wrote:
>('M','R','A','B') is correct. I appreciate the correction. I did not look to
>see the content of what I created, just the type!
>
 a = tuple("first")
 a
>('f', 'i', 'r', 's', 't')
 type(a)
>
>
>But I thought adding a comma would help and it does not!

Ah, the comma is for an expression. But you made a function call (well, 
"tuple" is a type and types are callable to make instances).

In a function call the parmeter separating commas have higher precedence 
than the commas with defines a tuple, you you've made a function call 
with one argument "first", not a function call with one _tuple_ argument 
("first",).

Consider:

foo(1, 2)

That supplies 2 arguments, not a single tuple.

As with other situations where the default precedence groups things in a 
different way from your intent, brackets get required here if you want 
to express a tuple.

This:

foo( (1,2) )

isn't notionally different from needing brakcets to express this:

(3+5) * 8

which means something else without its brackets.

Also, function calls allow you to include a trailing comma because it 
helps with code. Consider the following bigger call:

x = foo(
1,
2,
fred=5,
)

laid out on separate lines for readability (a little redundant here, but 
some complex function calls, or those exceeding the deired line length, 
are often folded this way).

Bu allowing a trailing comma we get consistent formatting and nicer 
diffs. If a trailing comma were forbidden, the dopping "fred=5" with 
produce a diff removing not just that line but also the comma on the 
preceeding line. Ugly and noisy.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Puzzling difference between lists and tuples

2020-09-20 Thread Avi Gross via Python-list
('M','R','A','B') is correct. I appreciate the correction. I did not look to
see the content of what I created, just the type!

>>> a = tuple("first")
>>> a
('f', 'i', 'r', 's', 't')
>>> type(a)


But I thought adding a comma would help and it does not!

>>> b = tuple("first",)
>>> b
('f', 'i', 'r', 's', 't')

Yet something this simple without invoking tuple(), works!

>>> c = 'first',
>>> c
('first',)

So I read the manual page and tuple takes an iterable as an argument and
treats a string as an iterator on characters! It is not a simple
initializer. 

I got around it, sort of, using n array with a single object of type string
in it so the iterator is iterating at a different level.

>>> d = ["first"]
>>> tuple(d)
('first',)
>>> tuple(["first"])
('first',)

I understand the design choice and can imagine there may be another function
that initializes a tuple more directly in some module.

Returning to lurking mode ...

-Original Message-
From: Python-list  On
Behalf Of MRAB
Sent: Sunday, September 20, 2020 7:35 PM
To: python-list@python.org
Subject: Re: Puzzling difference between lists and tuples

On 2020-09-20 23:59, Avi Gross via Python-list wrote:
> There is a simple and obvious way to make sure you have a tuple by
invoking the keyword/function in making it:
> 
 a=('first')
 type(a)
> 
> 
 a=("first",)
 type(a)
> 
> 
 a=tuple("first")
 type(a)
> 
> 
> That seems more explicit than adding a trailing comma. It also is a simple
way to make an empty tuple but is there any penalty for using the function
tuple()?
> 
[snip]
 >>> tuple("first")
('f', 'i', 'r', 's', 't')

Not the same as ("first",).

A simpler way to make an empty tuple is just ().
-- 
https://mail.python.org/mailman/listinfo/python-list

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


Re: Puzzling difference between lists and tuples

2020-09-20 Thread Greg Ewing

On 21/09/20 10:59 am, Avi Gross wrote:

a=tuple("first")
type(a)



That seems more explicit than adding a trailing comma.


It doesn't do what you want, though:

>>> a = tuple("first")
>>> print(a)
('f', 'i', 'r', 's', 't')

If you really want to use tuple() to create a 1-tuple without
using a trailing comma, you can do it this way:

>>> a = tuple(["first"])
>>> print(a)
('first',)

But this costs you both a list creation and a tuple() call.
On my machine it seems to be about 17 times slower than using
a trailing comma:

>>> timeit.repeat("tuple(['first'])")
[0.1774688908953, 0.1768788059062, 0.1768771102082, 
0.176763284033, 0.1768448921963]

>>> timeit.repeat("('first',)")
[0.0117392889055, 0.01156933400708, 0.01158800017473, 
0.01156976132486, 0.01157938358281]


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


Re: Puzzling difference between lists and tuples

2020-09-20 Thread MRAB

On 2020-09-20 23:59, Avi Gross via Python-list wrote:

There is a simple and obvious way to make sure you have a tuple by invoking the 
keyword/function in making it:


a=('first')
type(a)




a=("first",)
type(a)




a=tuple("first")
type(a)



That seems more explicit than adding a trailing comma. It also is a simple way 
to make an empty tuple but is there any penalty for using the function tuple()?


[snip]
>>> tuple("first")
('f', 'i', 'r', 's', 't')

Not the same as ("first",).

A simpler way to make an empty tuple is just ().
--
https://mail.python.org/mailman/listinfo/python-list


RE: Puzzling difference between lists and tuples

2020-09-20 Thread Avi Gross via Python-list
There is a simple and obvious way to make sure you have a tuple by invoking the 
keyword/function in making it:

>>> a=('first')
>>> type(a)


>>> a=("first",)
>>> type(a)


>>> a=tuple("first")
>>> type(a)


That seems more explicit than adding a trailing comma. It also is a simple way 
to make an empty tuple but is there any penalty for using the function tuple()?


-Original Message-
From: Python-list  On 
Behalf Of "???"
Sent: Saturday, September 19, 2020 11:39 PM
To: python-list@python.org
Subject: Re: Puzzling difference between lists and tuples

William Pearson  writes:

> ...
> for n in ('first'):
> print n
>
>
> ... but "f","i","r","s","t" in the second.

#+BEGIN_SRC: python
for n in ('first',):
print n
#+BEGIN_SRC

Then, that will print 'first'. And please use Python3...

Sincerely, Byung-Hee

-- 
^고맙습니다 _救濟蒼生_ 감사합니다_^))//
-- 
https://mail.python.org/mailman/listinfo/python-list

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


Re: importlib changes from py3.7 to py3.8

2020-09-20 Thread Eko palypse
Hello Greg,
thank you for your answer. I have checked it twice and the file is really
empty.
I even opened it with a hex editor - no content.
BUT in order to create a bug ticket I tried to make a mcve and I didn't
succeed.
I copied the function (load_plugins) 1 to 1, but outside my program it
works as it should.
So it seems that something inside my application is causing this problem.
Hmm ... Fortunately this is a pure python application so debugging should
lead to enlightenment. :-)

Thx
Eren

Am So., 20. Sept. 2020 um 02:06 Uhr schrieb Greg Ewing <
greg.ew...@canterbury.ac.nz>:

> On 19/09/20 7:51 am, Eko palypse wrote:
> > ValueError: source code string cannot contain null bytes
> >
> > Any idea what happened here?
>
> > Seems I've missed that __init__.py's aren't allowed to be empty anymore.
> > Adding a single # solved the issue.
>
> I just tried importing a package with an empty __init__.py in
> Python 3.8 and there was no problem:
>
> % ls -l empty
> total 0
> -rw-r--r--  1 greg  staff  0 20 Sep 12:00 __init__.py
> % python3
> Python 3.8.2 (default, Mar 23 2020, 11:36:18)
> [Clang 8.1.0 (clang-802.0.42)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import empty
>  >>>
>
> It looks more like the file in question wasn't completely empty,
> but had a null character in it for some reason. I expect that if
> you re-saved it as truly empty it would work. If not, you should
> probably report it as a bug.
>
> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list