Differences between revisions 9 and 10
Revision 9 as of 2005-01-06 15:29:25
Size: 5187
Editor: c-67-165-222-53
Comment: withdrew iterators instead of tuples argument parsing suggestion
Revision 10 as of 2005-01-07 18:59:15
Size: 5652
Editor: c-67-165-222-53
Comment:
Deletions are marked like this. Additions are marked like this.
Line 44: Line 44:
    ''I definitely agree that this is the right way to go with setdefault. Note though that setdefault should probably take *args and **kwds parameters so that the following would also be valid: {{{#!python
d.setdefault(function=list, [0]) }}} {{{#!python
d.setdefault(function=dict, a=0, b=1)
}}} It'd make the argument parsing a little bit more of a pain, but I think it'd make the function version of setdefault much more flexible. -- StevenBethard''


Other syntax ideas and feature ideas for Python 3.0.

TableOfContents()

Optional Static Typing / Adaptation

Lambda / Anonymous Methods / Closures

".." Sequences, Custom Infix Operators

Improved default value logic for Dictionaries

  • The setdefault() method is badly named and poorly designed. In a typical call, d.setdefault.(k, []).append(v), the list may be unnecessarily instantiated and discarded on every call. At a minimum, default value should be a new empty list instead of None:  d.setdefault(k).append(v) .

  • A more versatile idea is to realize that defaults generalize to the whole dictionary instead of an individual lookup. A call to setdefault would then change the whole dictionary's behavior when a key is not found:

    counts = {}
    counts.setdefault(value=0)
    for elem in data:
        counts[elem] += 1

    index = {}
    index.setdefault(function=list)
    for pageno, page in enumerate(pages):
        for line in page:
            for word in line.split():
                 index[word].append(line)
  • I definitely agree that this is the right way to go with setdefault. Note though that setdefault should probably take *args and **kwds parameters so that the following would also be valid:

       1 d.setdefault(function=list, [0]) 
    

       1 d.setdefault(function=dict, a=0, b=1)
    

    It'd make the argument parsing a little bit more of a pain, but I think it'd make the function version of setdefault much more flexible. -- StevenBethard

Better boolean logic

The and/or operators should only return boolean values. This makes their use less error-prone, less prone to abuse, and more closely match other languages. Also, it will simplify the underlying bytecode which currently inserts many POP_TOP instructions to complete conditionals. The need to insert these instructions also results in extra code paths and jump instructions. Overall, the language will become more intuitive, more reliable, simpler, and faster.

Disallow calling class methods from instances

Calling with a instance is almost never what you want. When it is done, the results are not especially readable, and the code suggests that it is doing something that it isn't:

{'a'=1}.fromkeys('poof') # what happened to 'a'? 

Simplify the syntax for raising exceptions

  • Eliminate string exceptions entirely.
  • Alway require instantation. IOW, prefer raise ValueError(dat) to raise ValueError, dat.

  • Require that all exceptions subclass from Exception.

  • Have Exception be a new-style class

Fix implementation of in-place operators

The current implementation will call __iadd__(), allowing it to do the in-place change, but then require that the method return the new value and then store it again. Ideally, all the responsibility for the update should lie with the __iadd__() method and it should return None. This simplifies the bytecode and eliminates some annoying behavior (such as a[0]+=1 succeeding and raising an error when a=([0],).

Remove the distinction between data and non-data decriptors

Having the distinction provides a tiny benefit but incurs a large cost in terms of implementation complexity and increasing the learning curve for descriptors. Using the presence or absence of a setter to distinquish the two is somewhat hackish and confuses the heck out of anyone first trying to master descriptors. Even after using descriptors for a while, that nuance remains an annoying distraction.

Reconsider the inclusion of __slots__ or re-evaluate its implementation

Guido has expressed that this is a highly popular, but badly misunderstood tool that is often used incorrectly.

  • If __slots__ is misspelled, there is no visible indication of failure.

  • The purpose of the tool is not to make it more difficult to assign attributes.

  • __slots__ do not inherit.

  • __slots__ complicates and slows the implementation of new-style classes.

Python3.0Suggestions (last edited 2010-05-16 23:17:52 by modemcable054)

Unable to edit the page? See the FrontPage for instructions.