preference tree like in jdk 1.4

Michael Chermside mcherm at destiny.com
Thu Feb 14 13:55:13 EST 2002


> i was thinking of an empty function:
> 
>   def empty(): pass
> 
> and then use it to build complex tree of preferences:
> 
>   pref = empty
>   pref.personalData = empty
>   pref.personalData.firstName = 'John'
>   pref.personalData.lastName = 'Cleese'
>   pref.windowSettings = empty
>   pref.windowSettings.windowA = empty
>   pref.windowSettings.windowA.x = 200
>   # and so on...
> 
> 

> to store or load this pref object, just use the pickle mechanism.
> 
> my question is, is there a simpler, more beautiful, elegant way?


Yes.

Instead of using an empty function, use something designed specifically 
for holding fields: a class!


 >>> class Empty:
	pass
 >>> pref = Empty()
 >>> pref.personalData = Empty()
 >>> pref.personalData.firstName = 'John'
 >>> pref.personalData.lastName = 'Cleese'
 >>> pref.windowSettings = Empty()
 >>> pref.windowSettings.windowA = Empty()
 >>> pref.windowSettings.windowA.x = 200

-- Michael Chermside

(Note: there may be fundamentally better solutions too; I just wanted to 
get away from mis-using functions.)






More information about the Python-list mailing list