dangerous class neighborhood

Avi Gross avigross at verizon.net
Fri Dec 28 13:13:51 EST 2018


[REAL SUBJECT: an assortment of nothings]

 

Python tricks?

 

Speaking from a tutorial forum, tricks can catch people trying to learn.

 

I am not sure of the history of the ellipsis object. 

 

>>> dir(...)

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

 

Would you believe it is syntactically valid to use 4 periods in a row by asking for the contents like this:

 

>>> ....__class__

<class 'ellipsis'>

 

My impression is that the main initial use for the ellipsis was as a placeholder in a numpy multidimensional array so you can specify which axis you want to get all of:

 

https://python-reference.readthedocs.io/en/latest/docs/brackets/ellipsis.html

 

I don’t want to start yet another discussion but there seem to be a range of ways to say you want something without quite saying what you want. We have mentioned the “pass” argument. We also have the naked colon as in:

 

new = old[ : ]

 

The above will make a shallow copy of a list. It is not really a new symbol but what happens when you take from:to and remove the from and the to and allow the defaults to take over. It gets murky as often underneath it all there are hidden objects like the slice:

 

>>> alpha = list(range(10))

>>> alpha

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

So you can explicitly index with a slice object or use the colon notation to get the same thing:

 

>>> alpha[1:5]

[1, 2, 3, 4]

>>> alpha[slice(1,5)]

[1, 2, 3, 4]

>>> alpha[1:10:2]

[1, 3, 5, 7, 9]

>>> alpha[slice(1,10,2)]

[1, 3, 5, 7, 9]

>>> alpha[5:]

[5, 6, 7, 8, 9]

>>> alpha[slice(5,)]

[0, 1, 2, 3, 4]

>>> alpha[slice(5,None)]

[5, 6, 7, 8, 9]

>>> alpha[:]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> alpha[slice(,)]

SyntaxError: invalid syntax

>>> alpha[slice(None)]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Note the placeholder is now the item called “None”

 

The ellipsis can be used here but causes errors.

 

And there are more subtle placeholders with meanings like DOES not exist. Numpy has a “nan” and Pandas has NaN and a missing date is NaT and beneath it all there are names for a missing int versus float versus who knows what.

 

These can be subtle ideas for a newcomer, even for people who come from languages that don’t have the distinctions. For that matter, there are ways to say something is infinite which is not the same as saying the maximum value you can store in N bits.

 

But I express some caution on calling some of these things tricks. Yes, of course they can be tricky. What they often are may well be considered additional functionality by way of abstractions. Clearly a key word like “pass” may be easier to understand than some of the others.

 

 

From: Abdur-Rahmaan Janhangeer <arj.python at gmail.com> 
Sent: Friday, December 28, 2018 3:46 AM
To: Avi Gross <avigross at verizon.net>
Cc: Python <python-list at python.org>
Subject: Re: dangerous class neighborhood

 

thanks, the 3 dots are more explicit, thought was another py trick

 

yours,

Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius




More information about the Python-list mailing list