问题描述
Python的一切都是对象。 我们确实找到了我们使用type()内置方法的对象的名称。
如果我按如下方式创建类
class Sample:
pass
a=Sample()
type(a)
returns as __main__.Sample
在这里,我希望将其打印为我选择的对象的名称,该如何做。
Ex: type({})
return Dict
我想命名自定义类的对象实例
1楼
一个可以使用模块替换类实例中的main的类,
即
class Sample():
__module__='custom'
type(Sample())返回custom.Sample
2楼
I think I figured it out with existing python packages,
i.e
in pandas, it has DataFrame class or object when you define variable to this object and type of it returns pandas.core.frame.DataFrame. Here DataFrame is the class and pandas.core.frame is the module name of the class
so one can define his object name with replacing __main__ only i.e as shown below
class Sample():
__module__='Custom'
a=Sample()
print(type(a))
prints==> < class Custom.Sample>