Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Python Dictionaries

From: al:bri:xtra at: 22-Sep-2001 19:47

> Python Dictionaries look like this: > > >>> rolodex = {'name' : 'Jason', 'email' : ['jasonic--nomadics--org']} > >>> print > 'Jason' > >>> rolodex['name'] > 'Jason' > > It seems the way to do this in REBOL is > >> rolodex: [name jason email [jasonic--nomadics--org]] > >> rolodex/name > == jason > etc > RQ1: With much bigger block, how do you keep track in these implicit pairs
of what is a 'key' and what is a 'value'. In Python the extra punctuation is more verbose, but always unambiguous One alternative is to use an object:
>> rolodex!: make object! [
[ Name: string! [ eMail: email! [ Site: url! [ ]
>> rolodex: make rolodex! [Name: "Jason" eMail: [jasonic--nomadics--org]] >> probe rolodex
make object! [ Name: "Jason" eMail: [jasonic--nomadics--org] Site: url! ]
> Named arguments for Python functions and classes allow one to set default
values very nicely. you can also call arguments in any order. The result is terrific readibility, because you don't have ask what argument is what, or deal with a predefined order. If you follow the pre-defined order, then you can drop the 'name=' part instead provide just the value. Another alternative is use 'parse as in the 'layout function in Rebol/View. The tutorial on VID at the Rebol site has examples.
> def name([arg, arg=value, *arg, **arg]): suite > > arg simple name > arg=value default if arg not passed > *arg collects extra positional args > **arg collects extra keyword args > > RQ2: Can anyone translate this into REBOL or suggest strategy for
attaining a keyword clarity. Perhaps it is not even desirable/necessary.. This one is a bit trickier, because Rebol doesn't really have variable arguments to functions. Instead, just pass a block of parameters and run it through 'parse rules (like Layout in View), or 'set to an object. For instance, a recursive function call: Stack/push/only reduce [Directory Files Pages] Sub: Generate Directory/:File set [Directory Files Pages] Stack/pop Andrew Martin ICQ: 26227169