Expert Mastery

Preview — 3 of 10 questions

What is the print order when the following code runs?

javascript
class Meta(type):
    def __new__(mcs, name, bases, ns):
        print(f"Meta.__new__ creating {name}")
        return super().__new__(mcs, name, bases, ns)

    def __call__(cls, *args, **kwargs):
        print(f"Meta.__call__ instantiating {cls.__name__}")
        return super().__call__(*args, **kwargs)

class Foo(metaclass=Meta):
    def __init__(self):
        print("Foo.__init__")

f = Foo()
AMeta.__new__ creating Foo, Foo.__init__, Meta.__call__ instantiating Foo
BMeta.__call__ instantiating Foo, Meta.__new__ creating Foo, Foo.__init__
CMeta.__new__ creating Foo, Meta.__call__ instantiating Foo, Foo.__init__
DFoo.__init__, Meta.__new__ creating Foo, Meta.__call__ instantiating Foo

What is the output of the following code?

javascript
class NonDataDesc:
    def __get__(self, instance, owner):
        return "from descriptor"

class DataDesc:
    def __get__(self, instance, owner):
        return "from descriptor"
    def __set__(self, instance, value):
        pass

class Example:
    non_data = NonDataDesc()
    data = DataDesc()

e = Example()
e.__dict__['non_data'] = "from instance dict"
e.__dict__['data'] = "from instance dict"

print(e.non_data)
print(e.data)
Afrom instance dict, from descriptor
Bfrom descriptor, from descriptor
Cfrom descriptor, from instance dict
Dfrom instance dict, from instance dict

What is the output of the following code?

javascript
import asyncio

async def async_range(n):
    for i in range(n):
        await asyncio.sleep(0)
        yield i

async def main():
    result = [x async for x in async_range(3)]
    print(result)

asyncio.run(main())
ATypeError: 'async_range' is not iterable
B[0, 1, 2]
CSyntaxError
D[0, 1, 2, 3]

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.