init

Subclassing dict: should dict.__init__() be called?

Subclassing dict: should dict.__init__() be called? Question: Here is a twofold question, with a theoretical part, and a practical one: When subclassing dict: class ImageDB(dict): def __init__(self, directory): dict.__init__(self) # Necessary?? … should dict.__init__(self) be called, just as a “safety” measure (e.g., in case there are some non-trivial implementation details that matter)? is there a …

Total answers: 5

Is there any reason to choose __new__ over __init__ when defining a metaclass?

Is there any reason to choose __new__ over __init__ when defining a metaclass? Question: I’ve always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def __init__(self, name, bases, dict): #do stuff here Is …

Total answers: 5

Can I use __init__.py to define global variables?

Can I use __init__.py to define global variables? Question: I want to define a constant that should be available in all of the submodules of a package. I’ve thought that the best place would be in in the __init__.py file of the root package. But I don’t know how to do this. Suppose I have …

Total answers: 4