[Patches] [ python-Patches-643443 ] dict.fromseq()

noreply@sourceforge.net noreply@sourceforge.net
Tue, 26 Nov 2002 01:40:37 -0800


Patches item #643443, was opened at 2002-11-25 11:29
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=643443&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Guido van Rossum (gvanrossum)
Summary: dict.fromseq()

Initial Comment:
Implements dict.sequpdate() as discussed on python-
dev:

def sequpdate(self, iterable, value=True):
    for k in iterable:
        self[k] = value
    return self


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

>Comment By: Just van Rossum (jvr)
Date: 2002-11-26 10:40

Message:
Logged In: YES 
user_id=92689

- I'm not sure whether PyObject_CallObject() is the right
idiom here. I would have expected to call cls->tp_new (if
not NULL), but I'm not sure either way.
- I would get rid of the loop counter: you're not using it.
for (;;) will do nicely
- you're XDECREF'ing values of which you know they're not NULL
- idem XINCREF. Also: I'm pretty sure you should incref d at
the end: I think it currently leaks.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-26 10:20

Message:
Logged In: YES 
user_id=80475

Fixed news item.

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

Comment By: Thomas Heller (theller)
Date: 2002-11-26 10:13

Message:
Logged In: YES 
user_id=11105

It seems the code and the docs are out of sync in 
fromseq2.diff.

The docs say "fromseq(sequence, value=True)" while the 
code does "fromseq(sequence, value=None)"

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-26 09:53

Message:
Logged In: YES 
user_id=80475

Done!  Revise patch attached.

It is now a class method and uses cls for the type of the 
new instance.  Added tests to demonstrate meaningful 
inheritance.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-11-26 02:15

Message:
Logged In: YES 
user_id=6380

Hm, but the convention for "alternate constructors" is that
the cls argument *is* used to determine the type of the new
instance. This way, the constructors are inherited in a
meaningful way by subclasses of dict.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-26 01:38

Message:
Logged In: YES 
user_id=80475

Attached revised patch incorporating all of the review 
comments.  Changed default to None.  Made a static 
method instead of a classmethod because cls was not 
used.  Doesn't return self.

Tests, docs, and news item revised accordingly.

Ready for BDFL pronouncement.

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-25 15:26

Message:
Logged In: YES 
user_id=92689

One more thing: I'd say the default value should be None.
This makes the idiom also useful to initialize dicts which
later get filled with proper values. This doesn't take
anything away from your original use case.

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-25 15:09

Message:
Logged In: YES 
user_id=92689

I see no reason why d.sequpdate() should return self.
d.update() doesn't either.

Also, as I suggested on python-dev, a fromseq() constructor
(as a class method) would be nice:

  >>> dict.fromseq('End Quit Stop Abort'.split())
  {'End': True, 'Quit': True, 'Stop': True, 'Abort': True}
  >>>

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

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