当前位置:首页>python>Python面向对象编程练习 2 命名空间和作用域

Python面向对象编程练习 2 命名空间和作用域

  • 2026-09-11 01:31:44
Python面向对象编程练习 2 命名空间和作用域

练习6

导入内置的datetime模块,并按如下所示显示该模块的命名空间(按字母顺序排序)。
提示:使用datetime模块的__dict__属性。
预期结果:
__name____doc____package____loader____spec____file____cached____builtins__sysMINYEARMAXYEARtimedeltadatetzinfotimedatetimetimezonedatetime_CAPI

练习7

下面给出了Product类。按如下所示显示该类的命名空间(__dict__属性的值)。
预期结果:
__module____init____repr__get_id__dict____weakref____doc__
import uuidclass Product:    def __init__(self, product_name, price):        self.product_id = self.get_id()        self.product_name = product_name        self.price = price    def __repr__(self):        return f"Product(product_name='{self.product_name}', price={self.price})"    @staticmethod    def get_id():        return str(uuid.uuid4().fields[-1])[:6] 

练习8

指定了Product类。创建了此类的一个名为product的实例。按如下所示显示该实例的命名空间(__dict__属性的值)。
预期结果:
{'product_name': 'Mobile Phone', 'product_id': '54274', 'price': 2900}
import uuidclass Product:    def __init__(self, product_name, product_id, price):        self.product_name = product_name        self.product_id = product_id        self.price = price    def __repr__(self):        return f"Product(product_name='{self.product_name}', price={self.price})"product = Product('Mobile Phone', '54274', 2900)

最新文章

随机文章