2
class fileInfo(dict):
    def __init__(self, name, typ, size = 1):
        self = {}
        self["name"] = name
        self["type"] = typ
        self["size"] = size

    def __getitem__(self, key):
        if key == "name":
            return dict.__getitem__(self, "name")+ "." + dict.__getitem__(self, "type")
        return dict.__getitem__(self, key)

I have created this class, but I have problems with the init function. When I try to initialize an object of this class, the init function returns me an empty dictionary.

What am I not understanding as far as the initialization function is concerned?

Cœur
  • 37,241
  • 25
  • 195
  • 267
docff
  • 23
  • 4

2 Answers2

2

This line doesn't do what you think it does:

    self = {}

That line creates a new object of type dict, and binds the local name self to it. This is wholly unrelated to the previous value of self, which is lost. And, since self is a local variable, the object created by this line will be marked for deletion when __init__() returns.

Try this instead:

super(fileInfo, self).__init__()

This neither creates nor destroys any objects, but modifies the object referred to by self.

Reference:

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Exaclty. `self = {}` just changed the meaning of `self` within `__init__`, so it no longer referred to the actual *self*, but to a new dict instead. – zvone Sep 14 '16 at 19:38
  • I understand now that I need to use super, but could you explain what `self = {}` does? – docff Sep 14 '16 at 19:44
  • You don't even need to call `super().__init__()`. – L3viathan Sep 14 '16 at 19:57
  • @L3viathan - can you elaborate? – Robᵩ Sep 14 '16 at 20:02
  • @Robᵩ Since we're subclassing `dict`, it is not necessary to call the `__init__` method of the superclass; it will behave like a dictionary anyways. – L3viathan Sep 14 '16 at 20:04
  • 1
    @L3viathan - do you have some support for that? Is that behavior documented? I can imagine a `dict` implementation that would require `__init__()` to be called. – Robᵩ Sep 14 '16 at 20:08
  • @Robᵩ OPs example [just works without](https://repl.it/D9nF/0). Of course there are cases where you want to call `__init__()`, e.g. when you're preinitializing it from keyword arguments. This is no special behaviour, just the fact that `dict.__init__` doesn't do anything special if you give it no arguments. – L3viathan Sep 14 '16 at 20:14
0

The __init__ method of any class is called once you create an instance of the class. You can't call it manually to instantiate the class, but use the class name.

Also, you are not really initializing a class that inherits it's methods and properties from dict. You need to use super for that. Also, this is a case where calling __init__ makes actual sense.

class fileInfo(dict):
    def __init__(self, name, typ, size = 1):
        super(fileInfo, self).__init__()

        self["name"] = name
        self["type"] = typ
        self["size"] = size

    def __getitem__(self, key):
        if key == "name":
            return dict.__getitem__(self, "name")+ "." + dict.__getitem__(self, "type")
        return dict.__getitem__(self, key)

file_info = fileInfo("file-name", "txt", 100)

print file_info["name"]

Output:

file-name.txt
Jarvis
  • 81
  • 3