gliese581g
gliese581g

Reputation: 453

Get name of type as a string

I have developed a function which operates on supplied input and if it doesn't find the type of input data as unicode it has to return that input data type unicode is required. I can do it by hard coding it to unicode but in future if I want to change required data type I dont want to change that string accordingly. Therefore I kept required data types of input variables in different file and if it doesn't match, it returns required data type from that file. But it returns something like

Required Data Type - <type 'unicode'>

Instead of <type 'unicode'> I want it to return just unicode. Please suggest.

Upvotes: 1

Views: 141

Answers (2)

Kiwi
Kiwi

Reputation: 2816

The following is an alternative and works with user-defined classes :

s.__class__.__name__

Example :

class A :
    pass

a = A()
type(a).__name__ # 'instance'
a.__class.__name # 'A'

s = u"string"
type(s).__name__ # 'unicode'
s.__class.__name # 'unicode'

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251051

Use the __name__ attribute of type:

>>> s = unicode(1)
>>> type(s).__name__
'unicode'

Upvotes: 8

Related Questions