Ellipsis - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: Ellipsis (/thread-18484.html) |
Ellipsis - Skaperen - May-19-2019 what is Ellipsis (the type with only one value expressed by the literal ...) used for? RE: Ellipsis - scidam - May-20-2019 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]
RE: Ellipsis - Skaperen - May-20-2019 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. RE: Ellipsis - scidam - May-20-2019 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... |