[ python-Feature Requests-1673203 ] add identity function

SourceForge.net noreply at sourceforge.net
Mon Mar 12 21:06:41 CET 2007


Feature Requests item #1673203, was opened at 2007-03-03 16:21
Message generated for change (Comment added) made by josiahcarlson
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1673203&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: paul rubin (phr)
Assigned to: Raymond Hettinger (rhettinger)
Summary: add identity function

Initial Comment:
Requested and assigned to Raymond at his suggestion:

http://groups.google.com/group/comp.lang.python/msg/603870361743c85c

There should be an identify function identity(x)=x.

I suggest it also take and ignore an optional second arg: identity(x1,x2)=x1.  The second arg can be useful in some generator expressions:

foo = (x for x in bar if condition(x) and identity(True, memoize(x))

That allows calling memoize (or some other function) on the selected elements in the genexp, and disposing of the returned value.  It's sort of like the const function (K combinator) to go along with the identity function's I combinator.  OK, the above is not really in the functional spirit, but it's been useful.

There could conceivably be also an actual const function const(k)=partial(identity,k) but I can't remember needing that in Python code.  The two-arg identity function (uncurried version of const) is probably enough.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2007-03-12 13:06

Message:
Logged In: YES 
user_id=341410
Originator: NO

Not all x line functions should be built into Python.  Further, Python's
standard syntax offers an infix operator that does the same thing (though
in slightly different order as described below, you can reorder with
minimal effort).

identity(X, Y) -> (Y and False) or X

Also, the only use-case that you are provided and that I can imagine, are
examples like you provide where one is changing state within a statement
(if, elif, while, etc.) or expression (generator, list comprehension,
conditional, etc.).

----------------------------------------------------------------------

Comment By: Alexander Belopolsky (belopolsky)
Date: 2007-03-05 06:21

Message:
Logged In: YES 
user_id=835142
Originator: NO

1. If this proposal is accepted, it will make sense to deprecate the use
of None as an identity function in map:

>>> map(None, range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2. Some other languages have an dyadic identity function that returns the
*second* argument. 

For example, K has : primitive:

  identity:(:)
  identity[1;2]
2

The rationale in K is that it is useful in an ammed function that replaces
entries of an an array with a result of a dyadic function applied to the
old and the supplied value and it is natural to have old value first:

  @[1 2 3;1;-;20]
1 -18 3
  @[1 2 3;1;:;20]
1 20 3

This rationale does not apply to Python, but in the absence of other
reasons to choose the order of arguments, Python may as well follow the
precedent. Does anyone know a less exotic language that has a dyadic
identity?




----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1673203&group_id=5470


More information about the Python-bugs-list mailing list