0

I have this class, which is, of course, just a working example:

class Intervals(dict):
    def __init__(self, *_):
        self.update({'P5': 7})

class Intervals(metaclass=Intervals):
    pass

I like the idea, because I don't need an instance that way.

Just, is now, every time when I call Intervals['P5'], __init__ run and a new dictionary built before a value can be returned?

  • 3
    Why are you using a meta class? And why do your classes share a name? What are you *actually trying to achieve?!* Surely you could have answered your own question with `print`, anyway? – jonrsharpe Jun 24 '15 at 08:09
  • Are you serious? Without the last two lines, `Intervals['P5']` would have to be `Intervals()['P5']`. That is also the reason why they both have the same name. Also, I just don't need an instance. But my question was whether that dictionary is built over and over again every time I want to retrieve a value. Because then, it wouldn't make sense to use a meta class or two classes having the same name... –  Jun 24 '15 at 08:25
  • Perfectly serious. The meta class doesn't have to have the same name as the class for this to work. And you still haven't explained *why* you're doing this; what is the requirement that has led to to this solution? – jonrsharpe Jun 24 '15 at 08:31
  • 2
    You have it wrong. A metaclass is supposed to yield a class when instantiated. Here, your metaclass yields a dict that cannot be instantiated. If you want to use syntaxes like `Intervals['P5']`, you should look into the [singleton pattern](http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python). – Vincent Jun 24 '15 at 08:36
  • But everything is obvious. I would like to have an object that is subscriptable and that I can use just like a dictionary without having an instance. The question is: Will a new dictionary be built every time I call `Intervals['P5']`? –  Jun 24 '15 at 08:41
  • @Vincent: Nice, thanks. –  Jun 24 '15 at 08:43
  • 1
    The answer is no, because the last two lines of code are equivalent to `Intervals = Intervals()`. – Vincent Jun 24 '15 at 08:48
  • That's quite nice, I guess, thanks. (My code looks good, everything makes sense, maybe it's a hack, but so far I haven't seen any flaws.) –  Jun 24 '15 at 09:02
  • *"I would like to have an object that is subscriptable and that I can use just like a dictionary without having an instance"* - so you really want to implement `__getitem__`/`__setitem__` on a class? You **still haven't said *why***. @Vincent is right, you are abusing the meta class, which should inherit from `type` and return a class. But my original point was that you could trivially answer your own question by adding e.g. `print('This is happening')` to `Instance.__init__` – jonrsharpe Jun 24 '15 at 09:37
  • I have also implemented `__call__`, `__contains__`, `__missing__`. And when `__getitem__` is called with a value the key is returned. But now that I see that `Intervals = Intervals()` is equivalent I am changing things a bit anyway. As far as the print part is concerned you are absolutely right. Didn't get the point... –  Jun 24 '15 at 10:28

1 Answers1

0

No, a new dictionary is not built every time you call Intervals['P5'].

Actually, your code is equivalent to:

class Intervals(dict):
    def __init__(self, *_):
        self.update({'P5': 7})

Intervals = Intervals()

You simply create an Intervals object (that is just a dict initialized with some values), and store it in a variable called Intervals, shadowing the Intervals class.

Vincent
  • 12,919
  • 1
  • 42
  • 64