Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ellipsis
#1
what is Ellipsis (the type with only one value expressed by the literal ...) used for?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
First of all, ellipsis is convenient triple of symbols (dots) to denote "something" when working with indexes.
It is ususally used in NumPy to denote multiple dimensions, e.g.
x = np.array(np.arange(10000)).reshape(10, 10, 10, 10) (x is array of shape 10x10x10x10, dim=4) x[:, :, :, :5] could be replaced with x[..., :5] (x[..., 5].shape = (10, 10, 10, 5)). So, ellipsis is useful when working with high dimensional data.

You can define your own behavior of the Ellipsis symbol, this, I think, its primary purpose.

class Container:
    def __getitem__(self, index):
        if index is Ellipsis:
            print("This is ellipsis")
            return 0
        else:
            return index
c = Container()
c[...]
c[3]
Output:
This is ellipsis 3
Reply
#3
i have a dictionary of option defaults. i had been using None in past code to indicate the option has no default. but in the current project i need to indicate there is no default along with that parameter must be specified on the command line (or it is an error). i was thinking maybe i could use Ellipsis for that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I am not sure I can understand you, may be this is because I am not a native English speaker. Are you talking about passing arguments to a function, or overriding dictionary methods, e.g. dict.__getitem__? Could you provide some code: where it was used None and where you want to use Ellipsis instead. If you need to override dict.__getitem__, you need to subclass UserDict instead of dict:

from collections import UserDict

class option(UserDict):
    def __getitem__(self, key):
        if key is Ellipsis:
            print("Do some stuff")
        else:
            return super().__getitem__(key)

z = option()
z.update({'one': 1})
z[...]
Hope that helps...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how is Ellipsis or ... used? Skaperen 9 1,940 Oct-04-2022, 10:17 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020