473,799 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

frozenset/subclassing/keyword args

Hello all,

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

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

from sets import ImmutableSet
class MSet1(Immutable Set):
def __init__(self, iterArg, myName="foo"):
ImmutableSet.__ init__(self, iterArg)
self.name = myName
class MSet2(frozenset ):
def __init__(self, iterArg, myName="foo"):
frozenset.__ini t__(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
3 1941
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
On Mon, 31 Oct 2005 19:31:33 GMT, "Mark E. Fenner" <Ho********@yah oo.com> wrote:
Hello all,

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

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

from sets import ImmutableSet
class MSet1(Immutable Set):
def __init__(self, iterArg, myName="foo"):
ImmutableSet.__ init__(self, iterArg)
self.name = myName
class MSet2(frozenset ):
def __init__(self, iterArg, myName="foo"):
frozenset.__ini t__(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
> 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(Immutable Set):
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.__ini t__(self, *iterArg)
self.name = myName

# broken
class MSet3(frozenset ):
def __init__(self, iterArg, myName="foo"):
frozenset.__ini t__(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8907
by: Fuzzyman | last post by:
I recently went through a bit of a headache trying to subclass string.... This is because the string is immutable and uses the mysterious __new__ method rather than __init__ to 'create' a string. To those who are new to subclassign the built in types, my experiences might prove helpful. Hopefully not too many innacuracies :-) I've just spent ages trying to subclass string.... and I'm very proud to say I finally managed it ! The...
3
1698
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary parts computed in point()'s __init__ function with their values based on the arglist. I want to compute with point instances as though they were native complex numbers, but I want to be able to
2
463
by: Stefan Behnel | last post by:
Hi! frozenset() doesn't behave as the other immutable empty data types in 2.4: ..>>> '' is '' True ..>>> () is () True ..>>> frozenset() is frozenset() False
10
4051
by: Will McGugan | last post by:
Hi, Are there any benefits in using a frozenset over a set, other than it being immutable? Will McGugan -- http://www.willmcgugan.com "".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
5
1826
by: Jacob Page | last post by:
I have released interval-0.2.1 at http://members.cox.net/apoco/interval/. IntervalSet and FrozenIntervalSet objects are now (as far as I can tell) functionality equivalent to set and frozenset objects, except they can contain intervals as well as discrete values. Though I have my own unit tests for verifying this claim, I'd like to run my code through actual set and frozenset unit tests. Does any such code exist? Is it in pure...
3
1376
by: insyte | last post by:
With assistance from Gabriel and Frederik (and a few old threads in c.l.p.) I've been making headway on my specialized datetime class. Now I'm puzzled by behavior I didn't expect while attempting to use some of the alternate datetime constructors. Specifically, it appears if I call GeneralizedTime.now() it calls the __new__ method of my class but treats keyword arguments as if they were positional. My class: class...
1
1182
by: Nitin | last post by:
Hi All I am trying to subclass an extension type in Python and add attributes to the new class but I keep getting errors. I read the "Extension Types" document on the Pyrex website but I couldn't get an answer from it. Here's the Spam extension type from Pyrex website: cdef class Spam:
3
1949
by: srinivasan srinivas | last post by:
Hi, I am getting an error while executing the following snippet. If i comment out method __repr__ , it works fine. class fs(frozenset):     def __new__(cls, *data):         data = sorted(data)         self = frozenset.__new__(cls, data)         self.__data = data         return self
0
1250
by: Terry Reedy | last post by:
srinivasan srinivas wrote: When posting problems like this, please include the Python version. If you ran this with 2.6, please run the following: x = frozenset('abc') y = frozenset('bcd') z = x.difference(y) print(id(x) != id(z))
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.