Python一切都是对象。我们确实使用内置的type()方法找到了对象的名称。
如果我按如下方式创建课程
params.require(:sale).permit(... , sale_items_attributes:[:id, :barcode])
在这里,我希望将其打印为所选对象的名称,该怎么做。
class Sample:
pass
a=Sample()
type(a)
returns as __main__.Sample
我要命名自定义类的对象实例
答案 0 :(得分:0)
一个人可以使用模块替换类实例类型中的 main ,
即
class Sample():
__module__='custom'
type(Sample())返回custom.Sample
答案 1 :(得分:0)
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>