简体   繁体   English

跟随__builtin__的是什么?

[英]What turns following to __builtin__?

I have following snippet and output 我有以下代码片段和输出

with metaclass:

def some(*args):
    return type(args)

__metaclass__ = some

class Foo:
   a = 'khkjh'


print Foo.__module__

Output: __builtin__ 输出: __builtin__

without metaclass:

class Foo:
   a = 'khkjh'

print Foo.__module__

Output: __main__ 输出: __main__

So, 所以,

What is __builtin__ ? 什么是__builtin__

why or how metaclass is affecting it? 元类为什么或如何影响它?

__builtin__ is the module that provides all built-in functions, exceptions, etc. __builtin__是提供所有内置函数,异常等的模块。

You're getting this returned from __module__ because the metaclass you're providing is essentially turning Foo into the tuple type: 您正在从__module__返回此信息,因为您提供的元类实际上将Foo转换为tuple类型:

>>> def some (*args):
...  return type(args)  # This returns <type 'tuple'>
...
>>> class Hmm(object):
...  __metaclass__ = some
... 
>>> class Foo(object):
...  pass
... 
>>> print Hmm
<type 'tuple'>
>>> print Foo
<class '__main__.Foo'>
>>> print tuple
<type 'tuple'>
>>> print tuple.__module__
__builtin__

As you can see Hmm is now the type tuple . 如您所见, Hmm现在是tuple类型。 The tuple type is provided by the __builtin__ module, hence the output you're seeing. tuple类型由__builtin__模块提供,因此您将看到输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM