Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Python's builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don't know in the builtins module:

__build_class__(...)     __build_class__(func, name, *bases, metaclass=None, **kwds) -> class      Internal helper function used by the class statement. 

What does this function do? Why must it be in builtins if it's internal? What is the difference to the type(name, bases, dict) function?

like image 365
u0b34a0f6ae Avatar asked Dec 02 '09 13:12

u0b34a0f6ae


People also ask

What is __ builtin __ in Python?

__builtin__ — Built-in objects See Built-in Functions and Built-in Constants for documentation. This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed.

Which statement Retreives all builtin objects in Python?

Check with the built-in function dir() You can get a list of names of built-in objects, such as built-in functions and constants, by passing the builtins module or __builtins__ to dir() . To make the output easier to read, use pprint.

How do I import a builtin?

import builtins def open(path): f = builtins. open(path, 'r') return UpperCaser(f) class UpperCaser: '''Wrapper around a file that converts output to upper-case. ''' def __init__(self, f): self. _f = f def read(self, count=-1): return self.


1 Answers

Compiling the PEP 3115 metaclass

Guido van Rossum said:

The PEP proposes that the class statement accepts keyword arguments, *args, and **kwds syntax as well as positional bases. This is a bit messy to compile and execute, but we already have this, of course, in the code for calling regular functions.

So I think it would be acceptable to this into a call to a new (hidden) built-in function, named __build_class__. Then that this class definition:

  class C(A, B, metaclass=M, other=42, *more_bases, *more_kwds):     ... 

would translate into this:

  C = __build_class__(<func>, 'C', A, B, metaclass=M, other=42, *more_bases, *more_kwds) 

where <func> is a function object for the class body.

like image 95
CodeJoust Avatar answered Oct 04 '22 20:10

CodeJoust