繁体   English   中英

如何为创建的类的实例定义自定义对象名称?

[英]How to define custom Object name for instance of created class?

Python的一切都是对象。 我们确实找到了我们使用type()内置方法的对象的名称。

如果我按如下方式创建类

class Sample:

      pass

a=Sample()

type(a)

returns as __main__.Sample

在这里,我希望将其打印为我选择的对象的名称,该如何做。

Ex: type({})

return Dict

我想命名自定义类的对象实例

一个可以使用模块替换类实例中的main的类,

class Sample():
    __module__='custom'

type(Sample())返回custom.Sample

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>

暂无
暂无

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

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