[Tutor] the use of the COLON

ThreeBlindQuarks threesomequarks at proton.me
Thu May 11 19:08:45 EDT 2023


When you say something WORKS, what do you mean?

Do you mean no error message?

Do you mean later code indicates it did what was expected?

This may sound like a dumb question but there seem to be a few anomalies in the code:

dummy_user_list: list=[["johnf", "jfabiani1947"]]

The first is this:

dummy_user_list:

Python does have a concept of starting an indented scope with a colon but not so much a concept of naming a region as in languages that have a GOTO or the ability to break out to a specific location:

On my machine, code like:

hello:

gets an error method about syntax.

Second, the word "list" is the name of a function and at best it can be very poor form to use it as a variable name. In the code you shared that does not generate an error for me either, it is NOT setting a variable called list to a value:

>>> dummy_user_list: list=[["johnf", "jfabiani1947"]]
>>> list
<class 'list'>
>>> print(list)
<class 'list'>

But if I leave out the meaningles colon part, it does overwrite:

>>> list=[["johnf", "jfabiani1947"]]
>>> list
[['johnf', 'jfabiani1947']]
>>> list[0]
['johnf', 'jfabiani1947']

And yes, I cannot make a list anymore with something like list(...) as the name is re-assigned.

Finally, what was the purpose of [["johnf", "jfabiani1947"]] versus just ["johnf", "jfabiani1947"] in Python? There are languages like R that have a [[ operator with somewhat different meaning but in Python, you asked for a list that contains a single other list which inside it has three items.

Back to your question, on MY SETUP, the code you mentioned only works in the sense of no error but does nothing useful. Anything after the : may as as well be a comment but not exactly.

Here is simpler code:

>>> hello: a=5
NameError: name 'a' is not defined
>>> a=1
>>> hello: a=5
>>>

The first command was run when no variable called "a" had ever been used in the session. It fails complaining about it so it obviously read the line. The next lines instantiate "a" to 1 and then it runs silently but does not change the value of a!

But it gets weirder when I wonder if "hello" is seen as a variable. I chose new undefined names:

>>> print(myname)
NameError: name 'myname' is not defined
>>> who="me"
>>> myname: who="Quark"
>>> print(who)
me
>>> print(myname)
Quark

So the above shows that at the start, there was no variable called "myname" and there was one of 'who' and yet the assignment to who was NOT done and yet passed along to 'myname'!!!

You could experiment more, but this behavior does not look like it was planned to be used by us! It almost looks like "who" is a local variable in the context of a region and the final value is passed to the name of the region!

I now tried a multi-line variation:

>>> x=1
>>> y=2
>>> z=3
>>> 
>>> mytown: z="three"
>>>   x="one"
>>>   y="two"
>>> 
>>> mytown
'three'
>>> x
'one'
>>> y
'two'
>>> z
3

It looks like the interpreter allowed multiple indented lines that altered x,y but that z on the same line as the colon behaved as before.

Another experiment shows only the first assignment is copied and all are in a sense ignored:

>>> myneighborhood: x=11;y=12;z=13
>>> myneighborhood
11
>>> x
'one'
>>> y
12
>>> z
13

But another egg sperm meant indicates the scope is LOCAL and changes last to local variables:

>>> myneighborhood: x=11;y=12;z=13; print(x,y,z)
one 12 13

But this test indicates it uses a global variable on the RHS.

>>> neighburrhood: x=x*3
>>> neighburrhood
'oneoneone'
>>> x
'one'

So, without doing more such experiments, I have no idea why this is allowed let alone what it does. I find it best to avoid using anything I do not understand!

Hopefully, someone else here can enlighten us.

Or, maybe it is an unfound bug!










Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, May 11th, 2023 at 3:29 PM, john fabiani <johnf at jfcomputer.com> wrote:


> Hi,
> I have never seen the following syntax. But it works! Can someone
> explain how it works or provide a link that have me understand what is
> happening?
> Thanks
> Johnf
> dummy_user_list: list=[["johnf", "jfabiani1947"]]
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list