TypeError: 'NoneType' object is not callable

Peter Otten __peter__ at web.de
Tue Jul 29 09:21:13 CEST 2014


Satish ML wrote:

> Hi,
> 
> TypeError: 'NoneType' object is not callable? Why this error and what is
> the solution? Code:
> class SuperMeta:
>     def __call__(self, classname, supers, classdict):
>         print('In SuperMeta.call: ', classname, supers, classdict,
>         sep='\n...') Class = self.__New__(classname, supers, classdict)
>         self.__Init__(Class, classname, supers, classdict)
> class SubMeta(SuperMeta):
>     def __New__(self, classname, supers, classdict):
>         print('In SubMeta.new: ', classname, supers, classdict,
>         sep='\n...') return type(classname, supers, classdict)
>     def __Init__(self, Class, classname, supers, classdict):
>         print('In SubMeta init:', classname, supers, classdict,
>         sep='\n...') print('...init class object:',
>         list(Class.__dict__.keys()))
> class Eggs:
>     pass
> print('making class')
> class Spam(Eggs, metaclass=SubMeta()):
>     data = 1
>     def meth(self, arg):
>         pass
> print('making instance')
> X = Spam()
> 
> print('data:', X.data)
> Output:
> making class
> In SuperMeta.call:
> ...Spam
> ...(<class '__main__.Eggs'>,)
> ...{'meth': <function Spam.meth at 0x0000000003539EA0>, '__module__':
> '__main__', '__qualname__': 'Spam', 'data': 1} In SubMeta.new:
> ...Spam
> ...(<class '__main__.Eggs'>,)
> ...{'meth': <function Spam.meth at 0x0000000003539EA0>, '__module__':
> '__main__', '__qualname__': 'Spam', 'data': 1} In SubMeta init:
> ...Spam
> ...(<class '__main__.Eggs'>,)
> ...{'meth': <function Spam.meth at 0x0000000003539EA0>, '__module__':
> '__main__', '__qualname__': 'Spam', 'data': 1} ...init class object:
> ['meth', '__module__', 'data', '__doc__'] making instance
> Traceback (most recent call last):
>   File "C:/Users/Satish/Desktop/Python/Metaclasses7.py", line 21, in
>   <module>
>     X = Spam()
> TypeError: 'NoneType' object is not callable

As I have no idea what you are up to I can only list some adjustments you 
probably need to make:

- __init__() and __new__() are spelt in lowercase.
- do not instantiate the metaclass:
  class Spam(Eggs, metaclass=SubMeta): 
      ...
- The metaclasses of Spam and Eggs must share a common base. 
  This can be achieved with
  class SuperMeta(type): 
      ...





More information about the Python-list mailing list