Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > LangWart: Method congestion from mutate multiplicty

Reply
Thread Tools

LangWart: Method congestion from mutate multiplicty

 
 
Mark Janssen
Guest
Posts: n/a
 
      02-10-2013
On Sun, Feb 10, 2013 at 6:29 AM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> Mark Janssen wrote:
>> I have to agree with Rick, I think requiring the user to explicitly
>> create a new object, which is already a good and widely-used practice,

>
> Perhaps so, but consider how you creates new objects in Python. Very rarely
> do you do so with an explicit call to the constructor. For example:
>
> n = 5 # Yes.
> # or
> n = int("5") # No.


Good, alright, this is the point where the concept of a unified object
model comes into play. (Actually, I'm going to say "unified data
model" (or UDM) from now on so as to avoid the confusion that you
point out below that Python seems to already have a "unified object
model" because everything derives from "Object". The point is
actually rather subtle.)

A unified data model as I define it, specifies a canonical atomic unit
(like the unit integer) and an abstract grouping construct in which
these atomic units can be arranged. By themselves, these two can
construct arbitrary levels of data structure complexity. Add the
ability to apply names to these levels, and you have a complete data
model for a happy programming environment. My work, to give you some
context, involves the invention of a "fractal graph" which is where
the name "unified object model" came from, because a fractal graph, I
argue can represent every part of the physical world at every scale.

(Note that everything in a computer is series of these "atomic" bits
organized by the machine into "words" (merely for sake of the
efficiency that such parallelization affords), yet we have these
human-language constructs such as lists and sets (or records, files,
arrays, etc) where *no such things exist in the computer*. Hence,
the usefulness of considering a unified data model as part of the
computer *science*.)

In your example above, you can verify, for example, that the identity
of 5 and int("5") is the same -- even though you're using a
constructor syntax, you're not *constructing* anything at all, which
you may know already (somewhere in the Python docs, Guido points out
that Python pre-constructs the first 100 or so integers as I recall.)
There is, in addition, an implicit constructor for integers, such that
saying 656565 in python will actually construct the integer as if you
said "int("656565")".

In any case, one never operates or concerns oneself with copies of
atomic elements because they are all the same. It's a subtle
meta-philosophical(?) point, not that different that that which occurs
in the science of physics regarding electrons and protons ("Is there
only one electron?":
http://en.wikipedia.org/wiki/One-electron_universe).

> py> import this
> [...]
> There should be one-- and preferably only one --obvious way to do it.


Yes, that is the reference.

>> Guessing method names is far
>> suboptimal to this simple, easy idiom. As for the point Chris was
>> making as to making all types one, I actually agree there too,

>
> Oh dear. Chris was being sarcastic. I thought that, even if the sarcasm
> wasn't obvious, his "Ook. Ook!" at the end should have given it away:


Yes, I was aware of his sarcasm. But I was actually wanting to agree
with the fundamental idea: that one could reduce all data types to 1
atomic unit and 1 grouping construct, and like set theory in
mathematics, derive everything else.

>> it's
>> just that in order to do that, python would need a unified object
>> model and it doesn't have one yet.

>
> I'm not sure what you mean by "unified object model", but I'm pretty sure
> that Python has one. Everything is an object, with a single[1] hierarchy of
> classes.


Hopefully the elucidation above clears up some of that confusion.

Mark
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      02-10-2013
On 2/10/2013 1:45 PM, Rick Johnson wrote:
> On Sunday, February 10, 2013 2:39:21 AM UTC-6, Terry Reedy wrote:
>> While it is true that sorted(iterable) is essentially
>>
>> def sorted(iterable):
>> tem = list(iterable)
>> tem.sort
>> return tem
>>
>> the body is not an expression and cannot be substituted in an
>> expression.

>
> Yes but the body can be compressed to this single line: "list(iterable).sort()"


That single line now evaluates to None, so that does not work.

>> Even if list mutation methods returned the list, which they do not and
>> for good reason,

>
> I am not proposing that in-place modification return the object.


It seems to me that you are, as that is the only way for
'list(iterable).sort()' to replace 'sorted(iterable)', as you proposed
both originally and above.

The reason sorted(iterable) was added is 'list(iterable).sort()', which
newbies would try, *does not work*. Sorted was added so people would not
have to write

tem = list(iterable)
tem.sort()
<statement using tem>
del tem

as they did previously, and instead could write

<statement using sorted(iterable)>

Reversed was added not only for the same reason, but also to avoid the
temporary list altogether when not actually needed, which it often or
usually is not.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      02-10-2013
On Mon, Feb 11, 2013 at 8:28 AM, Mark Janssen <(E-Mail Removed)> wrote:
> Yes, I was aware of his sarcasm. But I was actually wanting to agree
> with the fundamental idea: that one could reduce all data types to 1
> atomic unit and 1 grouping construct, and like set theory in
> mathematics, derive everything else.


There are many things that work fine in theory, but aren't practical.
You could theoretically rewrite any Python program in Ook (or its
non-G-rated cousin), but that doesn't mean that Ook's data model is
worth working with. You could write a Python-to-Ook compiler, perhaps,
for what that's worth. Proving these things possible may be of
curiosity value, but I wouldn't want to actually _work with_ such a
system.

A while ago I put together a language concep[1]t that, similarly,
started with nothing and let the programmer build from there. It
quickly proved to have one massive fundamental flaw: that two
programs, ostensibly written in the same language, could be utterly
and completely different. It'd be like treating Python and bash
scripts as the same language, given that the shebang at the top makes
them both execute just fine. If you reduce everything to nothing, you
(1) force the programmer to do a lot of unnecessary work, and (2)
allow two different programmers to do that work subtly differently and
thus create incompatible programs. (Python already has a little of
this, in that Py2 and Py3 files aren't guaranteed compatible; but
imagine if every source file were different.)

[1] Posted here if you care. http://rosuav.com/1/?id=683

ChrisA
 
Reply With Quote
 
Mark Janssen
Guest
Posts: n/a
 
      02-10-2013
On Sun, Feb 10, 2013 at 1:51 PM, Chris Angelico <(E-Mail Removed)> wrote:
> On Mon, Feb 11, 2013 at 8:28 AM, Mark Janssen <(E-Mail Removed)> wrote:
>> Yes, I was aware of his sarcasm. But I was actually wanting to agree
>> with the fundamental idea: that one could reduce all data types to 1
>> atomic unit and 1 grouping construct, and like set theory in
>> mathematics, derive everything else.

>
> There are many things that work fine in theory, but aren't practical.
> You could theoretically rewrite any Python program in Ook (or its
> non-G-rated cousin), but that doesn't mean that Ook's data model is
> worth working with.


Ah, but you're conflating a *data model* (which is already composed of
simple theoretical elements (like 1/0)) and a *programming language*,
which is composed of either an implicit or explicit data model
(usually the former) AND a set of transforms that operate on it.
IOW, I'm wanting to take something that is usually just inherited and
historical (and thereby taken for granted), and make it something to
look at. Traditional Data Structures in CompSci goes somewhat towards
this end, but doesn't quite take the idea to its ultimate, and that's
what I'm proposing with a unified data model.

mark
 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      02-10-2013
On Sunday, February 10, 2013 5:29:54 AM UTC-6, Steven D'Aprano wrote:
> Rick wrote:
> And you have missed my point, which is that reversed(), and sorted(), were
> not added to the language on a whim, but because they were requested, over
> and over and over again.


Well, well, this explains everything!

We don't add features because of logic, or because of consistency, or even because of good sense, we simply add them to appease the masses.

psst: Steven, maybe if the C and Java heads moan long enough we can get braced scoping, or, if the ruby guys pester us enough we can adopt the dollar sign to denote globals, the "@@" for class variables, and the "@" for instance variables! Oh yes! Because being loved is SO much more important than sticking to our philosophy of the Python Zen.

> "appended" is called list addition.
>
> newlist = oldlist + [item_to_append]


But where is the consistency?

Yes the syntactic sugar of the "plus sign" is concise, however, the "+" operator does not "pair" intuitively with the "append" method. Even IF you transformed the "+" operator into a named method like "add" (or call the magicmethod "__add__") it still fails to mesh properly with "append", and the two words utterly fail to intuitively establish an "in-place" versus "copy-mutate" relationship. Consider the English definitions of "add" and "append"when applied to sequence objects:

DEFINE "add"
Expand a sequence of "somethings" to include
additional "somethings" -- which may be another
sequence of N somethings. However the exact
location of the new "somethings" is not clearly
intuit-able from the word "add" alone!

DEFINE "append"
Expand a sequence of "somethings" to include an
additional "something". Here the exact location
is can be intuited as: "at the end".

> > flatten, flattened

>
> flatten is another often requested, hard to implement correctly, function..
> The only reason that Python doesn't have a flatten is that nobody can agree
> on precisely what it should do.


Steven, the definition of flatten (as relates to sequences) is very, VERY simple:

Return a new sequence that is the result of reducing
a nested sequence of sequences into a single depth
sequence.

> Like map, filter, reduce, etc. flatten is not sensibly implemented as a
> mutator method, but as a function.


Only because you, along with a few other high ranking members of this community (including the BDFL himself!) have this /aversion/ to true OOP paradigm.

Can you provide an example of how flatten "as a method" is incorrect versusflatten "as a function" is correct, or will this challenge be silently swept under the rug as so many before?

> > insert, inserted

>
> "inserted" is called addition, together with list slicing when needed.
> newlist = [item_to_insert] + oldlist
> newlist = oldlist[0:5] + [item_to_insert] + oldlist[5:



"inserted" is called "addition" by who?

If are implying that "seq.__add__()" employs the semantics of the English word "addition" and seq.insert(arg) is a thin wrapper around "oldlist[0:5]+[item]+oldlist[5:]", then fine. But how will someone intuit those two methods as have a "mutating pairs relationship" from the names alone? Not to mention that calling magic methods is anti-pythonic!

> > map, mapped
> > filter, filtered
> > reduce, reduced

>
> Those are nonsense. None of those are in-place mutator methods.


Well not in the current implementation of Python; but they could be if we wanted them to be. Applying mutations both "in-place" and "to a copy" are very helpful. For example, Ruby supplies both forms for many commonly used operations on arrays: (However i am not a fan of these "mutator pairs")

slice, slice!
sort, sort!
flatten, flatten!
collect, collect!
map, map!
uniq, uniq!
reject, reject!
reverse, reverse!
compact, compact!


Steven, just because you have yet to encounter such usage does not mean theusage is "non-sense"

seq.map(func)
new = seq.mapped(func)

seq.filter(lambda x: x<2)
new = seq.filtered(lambda x: x<2)


> Especially reduce, which reduces a list to a single item.


Nice to see you are paying attention! I am sure you already know this, although your wording was clumsy and suggests otherwise, but reduce does NOTHING to the list itself:

py> from operator import add
py> a = [1,2,3]
py> reduce(add, a)
6
py> a
[1, 2, 3]

reduce simply returns the result of the reduction; which is an Integer.

However it is my strong belief that the sum function should not exist when a reduce function does. Why? Because "sum([1,2,3])" is the equivalent of "reduce(operator.add, [1,2,3])"; the latter being significantly explicit and the former being a potential linguistical interpretation nightmare.

Now some might complain about this proposed removal of "sum" because the reduce function requires two arguments whereas the sum only needs one, and I say you're correct, however, this negative attribute is an artifact of a "global function nightmare architecture" and IF the core developers of Pythonwould accept the fine principals of true OOP paradigm, then the call wouldlook like this instead:

seq.reduce(operator.add)

This fine example of OOP style is much easier to grok than the either the sum or reduce global functions could ever wish to be.

> > extend, extended

>
> Again, "extended" is spelled list addition.
>
> Are you sure you've actually programmed in Python before? You seem awfully
> ignorant of language features.


So you are saying that "addition" is a "language feature"(sic). Can you please provide an example of "addition" as a language feature?

py> 'addition' in dir(list)
False

> [...]
> > My point was this: All mutate methods should mutate "in-place",

>
> Well duh. All mutator methods do mutate in-place, otherwise they wouldn'tbe
> mutator methods.


So "reversed()" and "sorted()" mutate in-place? Do you realize that these two functions are an extension of the list (or sub-types of list) interface and are in fact mutating a seq object in-place! Maybe not the sequence object you called the function on, but a seq object none-the-less!

> > if the
> > programmer wishes to create a mutated copy of the object, then the
> > programmer should /explicitly/ create a copy of the object and then apply
> > the correct mutator method to the copy.

>
> Been there, done that, it sucks. That's about a dozen steps backwards to a
> worse time in Python development.


Why? Because you have to type this

reversed = list(seq).reverse()

Instead of this:

reversed = reversed(seq)

*rolls-eyes*
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-11-2013
Mark Janssen wrote:

> A unified data model as I define it, specifies a canonical atomic unit
> (like the unit integer) and an abstract grouping construct in which
> these atomic units can be arranged. By themselves, these two can
> construct arbitrary levels of data structure complexity. Add the
> ability to apply names to these levels, and you have a complete data
> model for a happy programming environment. My work, to give you some
> context, involves the invention of a "fractal graph" which is where
> the name "unified object model" came from, because a fractal graph, I
> argue can represent every part of the physical world at every scale.


How can you breathe *way* up there in space?

http://www.joelonsoftware.com/articl...000000018.html

P.S. not all phenomena are fractal. The elbow joint, for instance, is just a
hinge, and not made of smaller elbow joints made of tinier elbow joints
made of even tinier elbow joints made of ...



--
Steven

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-11-2013
Dennis Lee Bieber wrote:

> On Mon, 11 Feb 2013 01:29:30 +1100, Steven D'Aprano
> <(E-Mail Removed)> declaimed the following in
> gmane.comp.python.general:
>
>>
>> Oh dear. Chris was being sarcastic. I thought that, even if the sarcasm
>> wasn't obvious, his "Ook. Ook!" at the end should have given it away:
>>
>> http://www.dangermouse.net/esoteric/ook.html
>>

>
> Ah... and here I thought it might have been a subtle reference to
> the Librarian of the Unseen University...
> http://en.wikipedia.org/wiki/Unseen_...sity#Librarian


Not so subtle. The creator of Ook obviously is a fan.


--
Steven

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-11-2013
Rick Johnson wrote:

> we can get the iterator for free. If however you want to control the
> iteration /without/ being locked into a loop, you can explicitly call:
>
> py> iter(seq)


> Or, if python employed /true/ OOP paradigm:
>
> py> Iterator(seq)


Today I learned that the difference between "true" OOP and everything else
is the presence of an initial capital letter.

Thank you Rick for your deep insight.


--
Steven

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      02-11-2013
> > > flatten, flattened
> >
> > flatten is another often requested, hard to implement correctly,
> > function. The only reason that Python doesn't have a flatten is
> > that nobody can agree on precisely what it should do.

>
> Steven, the definition of flatten (as relates to sequences) is
> very, VERY simple:
>
> Return a new sequence that is the result of reducing
> a nested sequence of sequences into a single depth
> sequence.


What should you get if you flatten

[[[1,2],[3,4]],[[5,6],[7,8]]]

Should the result be

[[1,2],[3,4],[5,6],[7,8]]

or

[1,2,3,4,5,6,7,8]

I've needed both cases, depending on the situation.

-tkc


 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      02-11-2013
On Sunday, February 10, 2013 7:30:00 AM UTC-6, Oscar Benjamin wrote:
> On 10 February 2013 04:53, Mark Janssen wrote:
> > [...]
> > I have to agree with Rick, I think requiring the user to explicitly
> > create a new object, which is already a good and widely-used practice,
> > should be the Only One Way to Do It.

>
> Why should I copy a potentially large data structure just to iterate
> over it in reverse order?


That's a good question, and the answer is: "Only a fool would make a copy of ANY data structure only to simply iterate over it; be it forwards or backwards or sideways".

> And why on earth would you want to remove
> the more efficient ways of doing this?


Well these "ways"(sic) might be more efficient, at this time, because the Python developers have defined them to be. This could be changed if they would drop the aversion to true OOP paradigm.

To make this work properly you would need to optimize the constructor of the sequence object. If the user presents the seq object (say for example a list) with variable that points to an already existing list like:

py> a = [1,2,3]
py> for x in list(a).reverse():
.... do_something

Python will not actually create a copy of the list because that would be foolish! Python would instead ITERATE over the existing object transparently. It's called OPTIMIZING CODE! By doing this we gain many things:

* We don't have these foolish "mutate"<->"copy mutate"
method "pairs" like: "seq.reverse()" and
"seq.reversed()"

* We are writing maintainable code by explicitly calling
"Sequence(seq).mutate()". The intent becomes obvious
even though Python may "optimize" our intentions "behind
the scenes".

> > Guessing method names is far suboptimal to this simple, easy idiom.

>
> There is no guessing. If the object has a __reverse__ method then it
> specifically advertises that it knows how to create an iterator that
> gives its values in reverse order. Otherwise __len__ and __getitem__
> are used.


Really.

And you know that simply from intuiting a seemingly unrelated method? Wow, i'd bet the detectives of many municipalities would love to rent some of your powers. What sort of esoteric rule book are you reading from my friend?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: LangWart: Method congestion from mutate multiplicty Jean-Michel Pichavant Python 0 02-11-2013 09:52 AM
Re: mutate dictionary or list Bruno Desthuilliers Python 0 09-07-2010 01:55 PM
mutate an object or create a new one? toton Java 23 11-02-2006 01:37 PM
naming convention for functions that mutate their arguments? mike p. Python 1 02-27-2004 02:18 AM
how to mutate a tuple? Carlo v. Dango Python 10 10-14-2003 03:32 PM



Advertisments