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

jepler at unpythonic.net jepler at unpythonic.net
Thu May 2 08:28:21 EDT 2002


On Thu, May 02, 2002 at 03:11:26PM +1200, Paul Foley wrote:
> On Wed, 1 May 2002 11:50:31 -0500, Jeff Epler wrote:
> 
> >> 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
> 
> Yes, but it also needs to be a distinguished from other objects.

I don't understand how an object cannot be "first class" when it can be
treated indistinguishably from other kinds of things.  Can you expand on
this claim?  Surely you don't mean that only things that have to be
decorated to be treated as a value (like &sub or #'function) or require a
special syntax to define (like /..../ or def blah(x): ...) can be
first-class.  

> > (as well as almost everything else in Python -- functions, instances,
> > bound methods, ...)  are first-class, because you can write
> 
> Python functions, instances, and methods are first class; REs aren't
> because there's no distinguished "regexp" object; they're either
> strings or instances.
> 
> >     def group(x): return r'\(' + x + r'\)
> 
> This is a string.

And, a bit below (unless I screwed up the original post) I produce a
compiled regexp using re.compile.  This is the object I'm claiming "is" a
regular expression and "is" first-class, since it can be "part of data
structures" (stored as an element of a sequence type, or in the attribute
of another object), "formed at runtime" (what I just did), "returned by
functions" (how I formed it) and "arguments to functions" (something the
next lines of code demonstrated).

> > 	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)
> 
> It's sometimes said (I've heard it, too), but it's wrong.  What you're
> passing around are pointers, not functions.

I don't think that this is relevant.  In fact, I bet it would be possible
for some C implementation to "inline" small functions in their "pointers".
For instance, a (perverse) architecture might have 128-bit function
pointers and 16-bit instruction words.  The top bit of a pointer is
0 if it refers to memory, and 1 if the bits 112..0 refer to 7 16-bit
instruction words.  Then, short functions like (hypot)
    MUL %f0, %f0
    MUL %f2, %f2
    ADD %f0, %f2
    SQRT %f0
    NOP; NOP; NOP
could "fit" within the "function pointer".   The natural calling sequence
    MOV %f0, x
    MOV %f2, y
    CALL hypot
or
    MOV %f0, x
    MOV %f2, y
    CALL %a0
would work, and it would be invisible to the programmer that the function
doesn't actually reside anywhere in particular in RAM (it could reside as
an "immediate" value in the current instruction stream, or it could reside
in the value of an address register.)

So, given that this interpretation will still allow the C language to
behave as it does, what justification (other than the syntax and
terminology of the language) makes it a "function pointer" and not a
"function value"?

Of course, you never pass any*thing* in a computer language -- you pass a
representation of the thing, or a value which corresponds to a
representation of a thing.  It's just not constructive to think of
programming a computer in these terms.  In the same way, in code like

    r = re.compile(".")
    f(r) # that line of code
I don't think "in that line of code, I'm passing a reference as an argument
to another reference treated almost as if it were a function (but of course
it's not, it's only bits)" I say "I'm passing the regular expression to a
function".

Jeff





More information about the Python-list mailing list