Personal tools
You are here: Home geek Plone Developer What's the best way to ask for an identity
Document Actions

What's the best way to ask for an identity

by Roberto Allende last modified 2006-07-06 18:25

A small chat with #python guys at freenode

(17:35:56) The topic for #python is: NO AOLSPEEK | The Python programming language | 
Over 3 lines? Paste -> http://deadbeefbabe.org/paste | #python.web for web | IRC bot? http://tinyurl.com/nkk27 |
2.5 alpha -> http://www.python.org/download/releases/2.5/


(17:37:48) r0ver: hi there, the statement: app.__class__ == <class 'OFS.Application.Application'> gives me a syntaxError,
even if i run app.__class__ i get <class 'OFS.Application.Application'> . Do you know how i shall do that ?
(17:38:22) r0ver: basically i want to compare app.__class__ with something and get True :) (just to write it shortly :) )

(17:38:54) werneck: r0ver: if you want to compare with
<class 'OFS.Application.Application'> , use repr(app.__class__) == "<class 'OFS.Application.Application'>"

(17:40:25) kingspawn: Wouldn't isinstance work for r0ver?
(17:40:50) werneck: r0ver: why do you want this ?

(17:42:07) ChrisLong: r0ver: import OFS.Application; print app.__class__ == OFS.Application.Application

(17:42:11) r0ver: werneck: for example, i've a tree and basically i want to know if an object is a node or a leaf
(17:43:14) werneck: r0ver: you can use isinstance(), but it's better to check if it has an attribute exclusive for node or leaf
(17:43:40) [OmegentooX]: What datatype is in your tree?
(17:44:34) r0ver: [OmegentooX]: ATFolder, LargePloneFolder and anything of the archetype's family :)

(17:52:30) r0ver: ChrisLong approach works fine:
(17:52:34) r0ver: import Products.ATContentTypes.content.folder
(17:52:34) r0ver: print folder[1].__class__ == Products.ATContentTypes.content.folder.ATFolder
(17:52:34) r0ver: True
(17:52:37) r0ver: thanks!
(17:53:19) ChrisLong: r0ver: but there is a better one: isinstance(folder[1], Products.ATContentTypes.content.folder.ATFolder)

(17:54:27) r0ver: ChrisLong: sorry to ask, but why do is it better ?
(17:56:25) ChrisLong: r0ver: a) it allows subclasses. b) no need to poke around with special attributes

After chatting with the zope guys i knew i've to use meta_type because zope products usually are not the same as a simple python class and one thing they have is an attribute called meta_type. So the line is:

context.getParentNode().meta_type == 'ATFolder'