"First-class objects" (was Re: Python vs. Perl, which is better to learn?)

Jeff Epler jepler at unpythonic.net
Wed May 1 12:50:31 EDT 2002


> Centuries ago, Nostradamus foresaw when "Mark McEahern" <marklists at mceahern.com> would write:
> > Could you give us a concrete example where the use of regular expressions in
> > Perl is superior to Python?
> 
On Wed, May 01, 2002 at 01:02:07AM -0400, Christopher Browne wrote:
> Well, in Perl, they're "first class objects," as it were, requiring no
> extra references to functions to get them to function, as a base part
> of the language syntax.

I always thought that "first class" meant a type of object that can
be passed as an argument in a function.[1]  In that sense, Python REs
(as well as almost everything else in Python -- functions, instances,
bound methods, ...)  are first-class, because you can write

    def group(x): return r'\(' + x + r'\)

    digit = "[0-9]"
    date = group(digit * 4 + "/" + 2*digit + "/" + 2 * digit) 
    date_re = re.compile(date)

    w = some_graphical_widget()

    w.set_valid_format(date_re)

In Perl, I'm pretty sure that
    &some_sub(/\(\d\d\d\d\/\d\d\/\d\d\)/);
does not pass a regular expression into some_sub, it passes the result
of matching that RE with the contents of $_.

Jeff
[1] From http://www.owlnet.rice.edu/~comp210/97fall/Labs/lab06/
	First-class functions

	The term first-class refers to the idea that values can be

	    * part of data structures,
	    * formed at run-time,
	    * arguments to functions, and
	    * returned by functions.
It's often said that functions are first-class in C (well, at least I've
heard it said) since they can be part of data structures, be passed to
functions or returned by them.  However, they cannot be formed at
runtime in C, so they may not be "first class" in the most stringent
sense.





More information about the Python-list mailing list