May-19-2019, 11:04 PM
May-20-2019, 01:18 AM
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.
You can define your own behavior of the Ellipsis symbol, this, I think, its primary purpose.
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
May-20-2019, 06:22 AM
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.
May-20-2019, 10:46 AM
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...