2013-04-10 14 views
2
class Car(): 
    def __init__(self,input): 
     self.carName = input 
    def showName(self): 
     print self.carName 
a = Car("bmw") 
print type(a) 
print a 

這將返回我我無法理解類<classname>(unicode):在Python中?

<type 'instance'> 
<__main__.Car instance at 0x7f188f38de60> 

class Car(unicode): 
    def __init__(self,input): 
     self.carName = input 
    def showName(self): 
     print self.carName 
a = Car("bmw") 
print type(a) 
print a 

<class '__main__.Car'> 
bmw 

據我瞭解,打印觸發的對象。 str()方法但unicode在這裏的意義是什麼?

回答

2

重要的是,您已經創建了一個類,該子類將內置類unicode劃分爲子類。很難看出你爲什麼想爲一個名爲Car的班級做這件事。

+0

我想試驗,是'__str__'方法,爲Unicode類定義?我很抱歉,但我無法找到正確的文檔。 – sharky 2013-04-10 05:37:14

+0

@sharky:當然'unicode'有'__str__'(和其他許多方法)。試試'dir(u'')'。 – NPE 2013-04-10 05:39:32

+0

謝謝,通過這樣做我也遇到了'__dict__'方法,+1 – sharky 2013-04-10 05:50:50