Python metaclass and the object base class

2024/9/24 4:27:47

After reading the excellent SO post, I tried crafting a module level metaclass:

def metaclass(future_class_name, future_class_parents, future_class_attrs):print "module.__metaclass__"future_class_attrs["bar"]="bar"return type(future_class_name, future_class_parents, future_class_attrs)__metaclass__=metaclassclass Foo(object):def __init__(self):print 'Foo.__init__'f=Foo()

This doesn't work (i.e. "module.metaclass" doesn't get printed) unless I remove the object base class of Foo. How come?

NOTE: I am using Python 2.6.1.

Answer

Inheriting from object automatically brings the type metaclass along with it. This overrides your module level __metaclass__ specification.

If the metaclass is specified at the class level, then object won't override it:

def metaclass(future_class_name, future_class_parents, future_class_attrs):print "module.__metaclass__"future_class_attrs["bar"]="bar"return type(future_class_name, future_class_parents, future_class_attrs)class Foo(object):__metaclass__ = metaclassdef __init__(self):print 'Foo.__init__'f=Foo()

See http://docs.python.org/reference/datamodel.html?highlight=metaclass#customizing-class-creation

https://en.xdnf.cn/q/71743.html

Related Q&A

Is this the equivalent of a copy constructor in Python?

Im reviewing some old python code and came accross this pattern frequently:class Foo(object):def __init__(self, other = None):if other:self.__dict__ = dict(other.__dict__)Is this how a copy constructor…

Fabric error No handlers could be found for logger paramiko.transport

Im not sure why Im getting this error thats terminating my connection. I updated paramiko-1.7.6 from 1.7.5 via easy_install.Im trying to setup Fabric to upload my Django app to my server. The error see…

How to mix unbalanced Datasets to reach a desired distribution per label?

I am running my neural network on ubuntu 16.04, with 1 GPU (GTX 1070) and 4 CPUs.My dataset contains around 35,000 images, but the dataset is not balanced: class 0 has 90%, and class 1,2,3,4 share the …

Is there a way to prevent pandas to_json from adding \?

I am trying to send a pandas dataframe to_json and I am having some issues with the date. I am getting an addtional \ so that my records look like Updated:09\/06\/2016 03:09:44. Is it possible to not…

Convert SQL into json in Python [duplicate]

This question already has answers here:return SQL table as JSON in python(15 answers)Closed 8 years ago.I need to pass an object that I can convert using $.parseJSON. The query looks like this:cursor.e…

Django Middleware - How to edit the HTML of a Django Response object?

Im creating a custom middleware to django edit response object to act as a censor. I would like to find a way to do a kind of search and replace, replacing all instances of some word with one that I c…

Disable or restrict /o/applications (django rest framework, oauth2)

I am currently writing a REST API using Django rest framework, and oauth2 for authentication (using django-oauth-toolkit). Im very happy with both of them, making exactly what I want.However, I have on…

find command with exec in python subprocess gives error

Im trying to execute the following command using subprocess module (python)/usr/bin/find <filepath> -maxdepth 1 -type f -iname "<pattern>" -exec basename {} \;But, it gives the fo…

Tensorflow import error on Pycharm (Mac)

Error msg (check the screenshot picture please):ImportError: cannot import name symbol_databaseError importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its …

ssl.SSLCertVerificationError for flask application OAuth login with keycloak

I have referred a sample hello-world flask app integrated with key-cloak login from https://gist.github.com/thomasdarimont/145dc9aa857b831ff2eff221b79d179a My client-secrets.json is as follows: {"…