By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,034 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,034 IT Pros & Developers. It's quick & easy.

frozenset/subclassing/keyword args

P: n/a
Hello all,

I was migrating some code from sets.ImmutableSet to frozenset and noticed
the following:

******code********
#!/usr/bin/env python

from sets import ImmutableSet
class MSet1(ImmutableSet):
def __init__(self, iterArg, myName="foo"):
ImmutableSet.__init__(self, iterArg)
self.name = myName
class MSet2(frozenset):
def __init__(self, iterArg, myName="foo"):
frozenset.__init__(self, iterArg)
self.name = myName
m1 = MSet1([1,2,3], myName = "donkey")
print m1
print m1.name

m2 = MSet2([1,2,3], myName = "kong")
print m2
print m2.name
*********end code**********

*********run**********
MSet1([1, 2, 3])
donkey
Traceback (most recent call last):
File "./setTest.py", line 22, in ?
m2 = MSet2([1,2,3], myName = "kong")
TypeError: frozenset() does not take keyword arguments
*********end run********

I'm missing something and couldn't find it in the docs.

Speaking of which, in the docs at the bottom of the description of the
builtin set/frozenset, there is a link to a page describing differences
between the builtin sets and the sets module sets. This link is broken
locally and on the python.org docs.
Locally, it reads:
file:///usr/share/doc/python-docs-2.4.2/html/lib/module-comparison-to-builtin-set.html

While it should read:
file:///usr/share/doc/python-docs-2.4.2/html/lib/comparison-to-builtin-set.html

Regards,
Mark
Oct 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Mark E. Fenner wrote:
Speaking of which, in the docs at the bottom of the description of the
builtin set/frozenset, there is a link to a page describing differences
between the builtin sets and the sets module sets. This link is broken
locally and on the python.org docs.
Locally, it reads:
file:///usr/share/doc/python-docs-2.4.2/html/lib/module-comparison-to-builtin-set.html

While it should read:
file:///usr/share/doc/python-docs-2.4.2/html/lib/comparison-to-builtin-set.html


A little further down the page it says "See About this document... for information on suggesting changes." If you click the link there it will tell you how to submit a doc bug which is the best way to get this fixed.

Kent
Oct 31 '05 #2

P: n/a
On Mon, 31 Oct 2005 19:31:33 GMT, "Mark E. Fenner" <Ho********@yahoo.com> wrote:
Hello all,

I was migrating some code from sets.ImmutableSet to frozenset and noticed
the following:

******code********
#!/usr/bin/env python

from sets import ImmutableSet
class MSet1(ImmutableSet):
def __init__(self, iterArg, myName="foo"):
ImmutableSet.__init__(self, iterArg)
self.name = myName
class MSet2(frozenset):
def __init__(self, iterArg, myName="foo"):
frozenset.__init__(self, iterArg)
self.name = myName
m1 = MSet1([1,2,3], myName = "donkey")
print m1
print m1.name

m2 = MSet2([1,2,3], myName = "kong")
print m2
print m2.name
*********end code**********

*********run**********
MSet1([1, 2, 3])
donkey
Traceback (most recent call last):
File "./setTest.py", line 22, in ?
m2 = MSet2([1,2,3], myName = "kong")
TypeError: frozenset() does not take keyword arguments
*********end run********

I'm missing something and couldn't find it in the docs.


Without researching it, I would guess that you have to override __new__
so as not to pass through the myName arg to the otherwise inherited and
called-with-all-arguments __new__ of the base class. You could take care
of the myName arg in the __new__ method too (by temporarily binding the
instance returned by frozenset.__new__ and assigning the name attribute
before returning the instance), or you can define __init__ to do that part.
See many various posted examples of subclassing immutable types.

Regards,
Bengt Richter
Oct 31 '05 #3

P: n/a
> Without researching it, I would guess that you have to override __new__
so as not to pass through the myName arg to the otherwise inherited and
called-with-all-arguments __new__ of the base class.
<snip>
Regards,
Bengt Richter


Bengt,

Thanks as always! Python's rabbit holes always go a little deeper then
you've currently gone. My one irritation is that it seems that the error
message could give some indication of where the problem lies. "bad args to
__new__", "immutable's bypass init", "black voodoo ahead" ... but I know,
efficiency, conciseness, other concerns, etc. etc. Doesn't mean I can't
gripe about it!

As you said, there are a number of threads on this. Consulting those gave a
quick three line solution, shown below.

Regards,
Mark

P.S. Here's what I should have been doing:

******************** start file *********************
#!/usr/bin/env python

from sets import ImmutableSet

class MSet1(ImmutableSet):
def __init__(self, iterArg, myName="foo"):
ImmutableSet.__init__(self, iterArg)
self.name = myName

# works
class MSet2(frozenset):
def __new__(cls, itrarg, *args, **kwargs):
return frozenset.__new__(cls, itrarg)

def __init__(self, iterArg, myName="foo"):
frozenset.__init__(self, *iterArg)
self.name = myName

# broken
class MSet3(frozenset):
def __init__(self, iterArg, myName="foo"):
frozenset.__init__(self, *iterArg)
self.name = myName

m1 = MSet1([1,2,3], myName = "donkey")
print m1
print m1.name

m2 = MSet2([1,2,3], myName = "mario")
print m2
print m2.name

m3 = MSet3([1,2,3], myName = "kong")
print m3
print m3.name
******************* end file ****************

************* sample run ******************
MSet1([1, 2, 3])
donkey
MSet2([1, 2, 3])
mario
Traceback (most recent call last):
File "./setTest.py", line 33, in ?
m3 = MSet3([1,2,3], myName = "kong")
TypeError: frozenset() does not take keyword arguments
************** end run ***********************

Nov 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.