Python 2.2.2 (#2, Nov 24 2002, 11:41:06)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright" , "credits" or "license" for more information. class pair(tuple):
.... def __init__(self,a ,b):
.... tuple.__init__( self, (a,b) )
.... a=pair(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: tuple() takes at most 1 argument (2 given)
What gives? (yes it works with a list, but i need immutable/hashable)
Simon Burton. 8 2217
On Mon, 18 Aug 2003 08:24:21 +0000, Duncan Booth wrote: Simon Burton <si****@webone. com.au> wrote in news:pa******** *************** *****@webone.co m.au:
> class pair(tuple): ... def __init__(self,a ,b): ... tuple.__init__( self, (a,b) ) ...> a=pair(1,2) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: tuple() takes at most 1 argument (2 given)>
What gives? (yes it works with a list, but i need immutable/hashable)
You need to override __new__ instead of __init__:
:) I need to grow a brain. thanks Duncan.
Simon.
Simon Burton: class pair(tuple): ... def __init__(self,a ,b): ... tuple.__init__( self, (a,b) )
What gives? (yes it works with a list, but i need immutable/hashable)
The problem is the immutability. This one one of the
new changes in 2.3 I still don't fully understand, but I do
know the solution is __new__ class pair(tuple):
.... def __new__(self, a, b):
.... return tuple((a, b))
.... pair(2,3)
(2, 3) x=_ type(x)
<type 'tuple'>
That should give you some pointers for additional searches.
Andrew da***@dalkescie ntific.com
In article <pa************ *************** *@webone.com.au >,
Simon Burton <si****@webone. com.au> wrote: Python 2.2.2 (#2, Nov 24 2002, 11:41:06) [GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2 Type "help", "copyright" , "credits" or "license" for more information.
class pair(tuple):... def __init__(self,a ,b): ... tuple.__init__( self, (a,b) ) ... a=pair(1,2)Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: tuple() takes at most 1 argument (2 given)
What gives? (yes it works with a list, but i need immutable/hashable)
You need to define __new__(); __init__() gets called *after* object
creation, when it's already immutable.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/
This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
In article <bh**********@s lb3.atl.mindspr ing.net>,
Andrew Dalke <ad****@mindspr ing.com> wrote: The problem is the immutability. This one one of the new changes in 2.3 I still don't fully understand, but I do know the solution is __new__
class pair(tuple):... def __new__(self, a, b): ... return tuple((a, b)) ... pair(2,3)(2, 3) x=_ type(x)<type 'tuple'>
That should give you some pointers for additional searches.
This works better:
class pair(tuple):
def __new__(cls, *args):
return tuple.__new__(c ls, args)
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/
This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Aahz: class pair(tuple): def __new__(cls, *args): return tuple.__new__(c ls, args)
Right. cls instead of self because it isn't passed the instance.
It would help me learn this new part of Python if I had
any use for it. :)
Though *args likely isn't what the OP wanted - I assume
that 'pair' takes only two elements.
Andrew da***@dalkescie ntific.com
"Andrew Dalke" <ad****@mindspr ing.com> wrote in message news:<bh******* ***@slb3.atl.mi ndspring.net>.. . Simon Burton:>> class pair(tuple): ... def __init__(self,a ,b): ... tuple.__init__( self, (a,b) )
What gives? (yes it works with a list, but i need immutable/hashable)
The problem is the immutability. This one one of the new changes in 2.3
<nitpick mode> Actually this was a change in 2.2 </nitpick mode>
__new__ is needed to acts on the creation of immutable objects and this
is the right way to use it; unfortunaly it gives room to plenty of abuses:
class YouThinkIamAStr ing(str):
def __new__(cls,arg ):
return 42
print YouThinkIamAStr ing("What's the answer?")
Yes, in Python you cannot modify the builtins, but still you have plenty
of rope to shoot in your foot ;)
Michele
On 18 Aug 2003 10:27:47 -0400, aa**@pythoncraf t.com (Aahz) wrote: In article <bh**********@s lb3.atl.mindspr ing.net>, Andrew Dalke <ad****@mindspr ing.com> wrote: The problem is the immutability. This one one of the new changes in 2.3 I still don't fully understand, but I do know the solution is __new__
> class pair(tuple): ... def __new__(self, a, b): ... return tuple((a, b)) ...> > pair(2,3) (2, 3)> x=_ > type(x) <type 'tuple'>>
That should give you some pointers for additional searches.
This works better:
class pair(tuple): def __new__(cls, *args): return tuple.__new__(c ls, args)
so far, just
class pair(tuple): pass
should do it, no? Unless you want to take the name as suggesting that
length 2 should be enforced. Don't know what other methods are planned,
but ISTM you get the vanilla __new__ for free. Or am I missing something?
Regards,
Bengt Richter
In article <bh**********@2 16.39.172.122>, Bengt Richter <bo**@oz.net> wrote: so far, just
class pair(tuple): pass
should do it, no? Unless you want to take the name as suggesting that length 2 should be enforced. Don't know what other methods are planned, but ISTM you get the vanilla __new__ for free. Or am I missing something?
Certainly; I'm just illustrating the principle if you wanted to do
something useful. ;-)
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/
This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michele Simionato |
last post by:
Let me show first how does it work for tuples:
>>> class MyTuple(tuple):
.... def __new__(cls,strng): # implicit conversion string of ints => tuple
.... return super(MyTuple,cls).__new__(cls,map(int,strng.split()))
>>> MyTuple('1 2')
(1, 2)
No wonder here, everything is fine. However, if I do the same for
lists I get the following:
|
by: Christoph Groth |
last post by:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iD8DBQBAJmKufx2/njzvX5URAipnAKCUco5ZCl85xJ8YboHHJM3RBIqiGwCfX3/P
tL1uWVMKntHThZ550BS32aw=
=4Ijl
-----END PGP SIGNATURE-----
|
by: Will McGugan |
last post by:
Hi,
Why is that a tuple doesnt have the methods 'count' and 'index'? It
seems they could be present on a immutable object.
I realise its easy enough to convert the tuple to a list and do this,
I'm just curious why it is neccesary..
Thanks,
|
by: Dave Opstad |
last post by:
I'm hoping someone can point out where I'm going wrong here. Here's a
snippet of a Python interactive session (2.3, if it makes a difference):
--------------------------------------
>>> class X(list):
.... def __init__(self, n):
.... v = range(n)
.... list.__init__(self, v)
....
>>> x = X(10)
|
by: James Stroud |
last post by:
Hello All,
Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some
subtle logic? Why does print not work the same way as raise? Both are
statements. Why does raise need to be so special?
py> sometup = 1,2
py> print sometup
(1, 2)
py> print 1,2,3, sometup
| |
by: groups.20.thebriguy |
last post by:
I've noticed that there's a few functions that return what appears to
be a tuple, but that also has attributes for each item in the tuple.
For example, time.localtime() returns a time.time_struct, which looks
like a tuple but also like a struct. That is, I can do:
>>> time.localtime()
(2006, 1, 18, 21, 15, 11, 2, 18, 0)
>>> time.localtime()
21
>>> time.localtime().tm_hour
|
by: Alan Isaac |
last post by:
I am probably confused about immutable types.
But for now my questions boil down to these two:
- what does ``tuple.__init__`` do?
- what is the signature of ``tuple.__init__``?
These questions are stimulated by
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439
Looking at that, what fails if I leave out the following line? ::
|
by: abcd |
last post by:
I wanted to extend tuple but ran into a problem. Here is what I
thought would work
class MyTuple(tuple):
def __init__(self, *args):
tuple.__init__(self, args)
x = MyTuple(1,2,3,4)
That gives me...
|
by: Pradnyesh Sawant |
last post by:
Hello,
I have a pyqt4 code in which i'm trying the signal/slot mechanism. The
(stripped) code is as follows:
class D(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
tpl = ("Primary", "priSec")
print "tpl:", tpl
self.emit(QtCore.SIGNAL("setLabel"), tpl)
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |