Defining our own types?

Kay Schluehr kay.schluehr at gmx.net
Wed Aug 16 10:29:44 EDT 2006


Michael Yanowitz wrote:
> Hello:
>
>    I know this will probably turn about to be another dumb question
> and I know I have asked a few really dumb ones lately on this list,
> but I am hoping otherwise this time:
>
> suppose I type:
> ip = 123.45.67.89
>   (no quotes)
>     - is it possible (maybe by catching an exception), to have this
> automatically converted to an ip address and maybe have the exception
> convert this into:
>   ip = make_ip_address (123, 45, 67, 89)

This will fail already on the parser level. The tokenizer recogizes
'123.45' as a number as well as '.67' and '.89' and can't fit them
together to something meaningfull. So you must adapt the tokenizer to
accept '123.45.67.89' as a number and handle the corresponding NUMBER
token by visiting the syntax tree later on i.e. returning either a
NUMBER or a function call.

I've written a framework that lets you do this [1] but be aware that
you actually create a new language. Personally I don't think it's worth
the effort and would define an IP object instead:

ip = IP (123, 45, 67, 89)

and/or

ip = IP ("123.45.67.89")

Regards,
Kay

[1] http://www.fiber-space.de/EasyExtend/doc/EE.html




More information about the Python-list mailing list