What is the purpose of `__metaclass__ = type`?

Mechanical snail

Python (2 only?) looks at the value of variable __metaclass__ to determine how to create a type object from a class definition. It is possible to define __metaclass__ at the module or package level, in which case it applies to all subsequent class definitions in that module.

However, I encountered the following in the flufl.enum package's __init__.py:

__metaclass__ = type

Since the default metaclass if __metaclass__ is not defined is type, wouldn't this have no effect? (This assignment would revert to the default if __metaclass__ were assigned to at a higher scope, but I see no such assignment.) What is its purpose?

Blckknght

In Python 2, a declaration __metaclass__ = type makes declarations that would otherwise create old-style classes create new-style classes instead. Only old-style classes use a module level __metaclass__ declaration. New-style classes inherit their metaclass from their base class (e.g. object), unless __metaclass__ is provided as a class variable.

The declaration is not actually used in the code you linked to above (there are no class declarations in the __init__.py file), but it could be. I suspect it was included as part of some boilerplate that makes Python 2 code work more like Python 3 (where all classes are always new-style).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the difference between __metaclass__ attribute and metaclass kwarg

What is the purpose of the unit type in Rust?

What is the purpose of `boolean` type in JVM?

What is the Purpose of the Item type in Foundation

What is the purpose of {} in the returning type of a function?

What is the purpose of casting into "object" type?

What is the purpose of a type lifetime bound?

What is the purpose of a restricting the type of generic in a method?

What is the meaning and purpose of this assignment in a type declaration in this example?

What is the purpose of Content-Type at response header?

What is the purpose of including the type in its definition in haskell?

What is the use/purpose of primitive type classes?

What is the purpose of type definitions? When is it useful?

What is the purpose of declaring typealias for generic type parameter

What is the purpose of generics before return type

What is the purpose of IServiceProvider.GetService(Type serviceType)?

What's the purpose of a blank identifier in a type parameter?

__metaclass__ in Python 3

What is the purpose of a placeholder type in a trailing-return-type?

What is the purpose of defining an interface type variable instead derived class type?

What is the purpose of .*\\?

What is the purpose of "?"

What is the purpose of using type parameters for an empty trait when extending it?

What is the purpose of service fabric cluster node type app port range?

For what purpose does java have a float primitive type?

What is the purpose of the `Exact<T>` type that @graphql-codegen creates?

What is the purpose of grant_type parameter in OAuth 2 Authentication

What is the purpose of a question mark after a type (for example: int? myVariable)?

What is the purpose of the password grant type (ROPC) in OAuth2?