shaunakde
shaunakde

Reputation: 3106

TypeError Python Class

I have a library (caffe) that has the following definition:

class NetSpec(object):
    def __init__(self):
        super(NetSpec, self).__setattr__('tops', OrderedDict())

    def __setattr__(self, name, value):
        self.tops[name] = value

    def __getattr__(self, name):
        return self.tops[name]

    def to_proto(self):
        names = {v: k for k, v in self.tops.iteritems()}
        autonames = {}
        layers = OrderedDict()
        for name, top in self.tops.iteritems():
            top.fn._to_proto(layers, names, autonames)
        net = caffe_pb2.NetParameter()
        net.layer.extend(layers.values())
        return net

When I try to call it using n = caffe.NetSpec() I get the following error:

  File "../../python/caffe/layers.py", line 84, in __init__
    super(NetSpec, self).__setattr__('tops', OrderedDict())

TypeError: must be type, not None

I checked SO-9698614,SO-576169 and SO-489269 but they did not lead to a solution. My class is a new type class and I could not see why it was not working.

Full trace:

Traceback (most recent call last):

  File "<ipython-input-72-694741de221d>", line 1, in <module>
    runfile('/home/shaunak/caffe-pr2086/examples/wine/classify.py', wdir='/home/shaunak/caffe-pr2086/examples/wine')

  File "/home/shaunak/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)

  File "/home/shaunak/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)

  File "/home/shaunak/caffe-pr2086/examples/wine/classify.py", line 26, in <module>
    f.write(str(logreg('examples/hdf5_classification/data/train.txt', 10)))

  File "/home/shaunak/caffe-pr2086/examples/wine/classify.py", line 18, in logreg
    n = caffe.NetSpec()

  File "../../python/caffe/layers.py", line 84, in __init__
    super(NetSpec, self).__setattr__('tops', OrderedDict())

TypeError: must be type, not None

Upvotes: 1

Views: 923

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121486

Somehow you managed to bind NetSpec to None somewhere:

>>> super(None, object())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be type, not None

The error indicates that the NetSpec global is bound to None.

You could also bypass the NetSpec.__setattr__ method by going directly to the instance __dict__ attribute:

class NetSpec(object):
    def __init__(self):
       self.__dict__['tops'] = OrderedDict()

From the code you shared it could be that this is the culprit:

from .layers import layers, params, NetSpec

This imports caffe.layers but rebinds caffe.layers to the Layers() instance. This could then trigger Python to delete the module again as there are no other references to it yet (depending on when and how the sys.modules reference is created), causing all globals to be rebound to None (including NetSpec).

Upvotes: 2

Related Questions