(Pdb) myclass
MyClass( 0, 0, 'A string', 123.45)
(Pdb) copy.copy(mycla ss)
*** TypeError: TypeError('__ne w__() takes at least 4 arguments (2 given)',)
I see 4 arguments (actually, 5 because Python is passing cls invisibly to
__new__). Does anyone have an idea what is going on here?
I have not defined __new__ in MyClass above. I can make the problem go away
on one platform by defining __new__ as
return MyClass(self, self.arg1, self.arg2, self.arg3)
but I understand that return value to be the default, so I don't see why
defining that method makes a difference. On another platform, that
definition causes a different problem (I seem to be getting None as the
return value of the copy in some cases).
By the way, I have simplified somewhat the code in the explanation. In case
it might matter, know that there are actually two classes that exhibit this
problem. In one, there is actually a fourth argument in the __new__ method
that has a default value (the float above), which is why the error message
says that __new__ expects *at least* 4 arguments. In the other, the last
argument is a parg collector.
I have also had trouble pickling these classes. I surmise that pickle uses
copy.
--
Jeffrey Barish 7 1773
On Tue, 22 Apr 2008 11:13:43 -0600, Jeffrey Barish wrote:
By the way, I have simplified somewhat the code in the explanation.
Please simplify the code to a minimal example that still has the problem
and *show it to us*. It's hard to spot errors in code that nobody except
you knows.
Marc 'BlackJack' Rintsch wrote:
Please simplify the code to a minimal example that still has the problem
and *show it to us*. It's hard to spot errors in code that nobody except
you knows.
Here it is:
import copy
class Test(int):
def __new__(cls, arg1, arg2):
return int.__new__(cls , arg1)
def __init__(self, arg1, arg2):
self.arg2 = arg2
if __name__ == '__main__':
t = Test(0, 0)
t_copy = copy.copy(t)
Traceback (most recent call last):
File "copytest.p y", line 12, in <module>
t_copy = copy.copy(t)
File "/usr/lib/python2.5/copy.py", line 95, in copy
return _reconstruct(x, rv, 0)
File "/usr/lib/python2.5/copy.py", line 322, in _reconstruct
y = callable(*args)
File "/usr/lib/python2.5/copy_reg.py", line 92, in __newobj__
return cls.__new__(cls , *args)
TypeError: __new__() takes exactly 3 arguments (2 given)
--
Jeffrey Barish
Jeffrey Barish wrote:
Marc 'BlackJack' Rintsch wrote:
>Please simplify the code to a minimal example that still has the problem and *show it to us*. It's hard to spot errors in code that nobody except you knows.
Here it is:
import copy
class Test(int):
def __new__(cls, arg1, arg2):
^^^^^^^
The exception is saying that copy.copy is not providing this 3rd
argument. I might be a bit dense, but what would arg2 be for? Isn't
arg1 supposed to be the object instance you are copying from? If so,
arg2 is superfluous.
En Thu, 24 Apr 2008 00:32:37 -0300, Michael Torrie <to*****@gmail. com>
escribió:
Jeffrey Barish wrote:
>Marc 'BlackJack' Rintsch wrote:
>>Please simplify the code to a minimal example that still has the problem and *show it to us*. It's hard to spot errors in code that nobody except you knows.
Here it is:
import copy
class Test(int): def __new__(cls, arg1, arg2):
^^^^^^^
The exception is saying that copy.copy is not providing this 3rd
argument. I might be a bit dense, but what would arg2 be for? Isn't
arg1 supposed to be the object instance you are copying from? If so,
arg2 is superfluous.
I agree. In case arg2 were really meaningful, use __getnewargs__ (see )
import copy
class Test(int):
def __new__(cls, arg1, arg2):
return int.__new__(cls , arg1)
def __init__(self, arg1, arg2):
self.arg2 = arg2
def __getnewargs__( self):
return int(self), self.arg2
pyt = Test(3, 4)
pyprint type(t), t, t.arg2
<class '__main__.Test' 3 4
pyt_copy = copy.copy(t)
pyprint type(t_copy), t_copy, t_copy.arg2
<class '__main__.Test' 3 4
But note that subclassing int (and many other builtin types) doesn't work
as expected unless you redefine most operations:
pyx = t + t_copy
pyprint type(x), x
<type 'int'6
pyt += t_copy
pyprint type(t), t
<type 'int'6
--
Gabriel Genellina
En Thu, 24 Apr 2008 01:21:15 -0300, Gabriel Genellina
<ga*******@yaho o.com.arescribi ó:
I agree. In case arg2 were really meaningful, use __getnewargs__ (see )
I forget to include the link: <http://docs.python.org/lib/pickle-inst.html>
--
Gabriel Genellina
On Apr 23, 9:48*pm, Jeffrey Barish <jeff_bar...@ea rthlink.netwrot e:
>
Here it is:
import copy
class Test(int):
* * def __new__(cls, arg1, arg2):
* * * * return int.__new__(cls , arg1)
* * def __init__(self, arg1, arg2):
* * * * self.arg2 = arg2
if __name__ == '__main__':
* * t = Test(0, 0)
* * t_copy = copy.copy(t)
First off, inheriting from a basic builtin type such as int and
changing its constructor's signature is not typical; you should
rethink your design unless you know what you're doing.
One way to make this work is to define the special __copy__ method
[1], specifying explicitly how to create a copy of a Test instance:
class Test(int):
...
def __copy__(self):
return Test(int(self), self.arg2)
The copy.copy() function looks for this special method and invokes it
if it's defined. Normally (i.e. for pure Python classes that don't
subclass a builtin other than object) copy.copy() is smart enough to
know how to create a copy without an explicit __copy__ method, so in
general you don't have to define it for every class that has to be
copyable.
Traceback (most recent call last):
* File "copytest.p y", line 12, in <module>
* * t_copy = copy.copy(t)
* File "/usr/lib/python2.5/copy.py", line 95, in copy
* * return _reconstruct(x, rv, 0)
* File "/usr/lib/python2.5/copy.py", line 322, in _reconstruct
* * y = callable(*args)
* File "/usr/lib/python2.5/copy_reg.py", line 92, in __newobj__
* * return cls.__new__(cls , *args)
TypeError: __new__() takes exactly 3 arguments (2 given)
The traceback is not obvious indeed. It turns out it involves calling
the arcane __reduce_ex__ special method [2] defined for int, which
returns a tuple of 5 items; the second is the tuple
(<class '__main__.Test' >, 0) and these are the arguments passed to
Test.__new__. So another way of fixing it is keep Test.__new__
compatible with int.__new__ by making optional all arguments after the
first:
class Test(int):
def __new__(cls, arg1, arg2=None):
return int.__new__(cls , arg1)
# don't need to define __copy__ now
from copy import copy
t = Test(0, 0)
assert copy(t) == t
As a sidenote, your class works fine without changing anything when
pickling/unpickling instead of copying, although pickle calls
__reduce_ex__ too:
from pickle import dumps,loads
t = Test(0, 0)
assert loads(dumps(t)) == t
Perhaps someone more knowledgeable can explain the subtle differences
between pickling and copying here.
George
[1] http://docs.python.org/lib/module-copy.html
[2] http://docs.python.org/lib/node320.html
George Sakkis wrote:
First off, inheriting from a basic builtin type such as int and
changing its constructor's signature is not typical; you should
rethink your design unless you know what you're doing.
Nah, I would never claim to know what I'm doing. However, I have to say
that I have been finding this technique very useful. When I started
developing this program, I used an int. Then I discovered that I needed to
have a string associated with the int. By subclassing int to add a string,
I managed to make the change transparent to the code I had already written.
Only the new code that needed the associated string knew that it was
available. In another case, I subclassed str so that I could have a long
form for a string (e.g., a full name attached to the surname). Are these
applications of subclassing bad form? What is the motivation for your
warning?
One way to make this work is to define the special __copy__ method
[1], specifying explicitly how to create a copy of a Test instance:
Normally (i.e. for pure Python classes that don't
subclass a builtin other than object) copy.copy() is smart enough to
know how to create a copy without an explicit __copy__ method, so in
general you don't have to define it for every class that has to be
copyable.
Yes, I noted in my original posting (which seems to have fallen off this
thread) that the __copy__method solved the problem (at least on one
platform). However, I was wondering why it was necessary when what I was
defining was supposedly the default action. Thanks for your explanation.
The traceback is not obvious indeed. It turns out it involves calling
the arcane __reduce_ex__ special method [2] defined for int, which
returns a tuple of 5 items; the second is the tuple
(<class '__main__.Test' >, 0) and these are the arguments passed to
Test.__new__. So another way of fixing it is keep Test.__new__
compatible with int.__new__ by making optional all arguments after the
first:
This suggestion is very interesting. It seems to be an alternative to the
solution suggested by Gabriel. The reference that Gabriel provided
includes the statement:
Instances of a new-style type C are created using
obj = C.__new__(C, *args)
where args is the result of calling __getnewargs__( ) on the original
object; if there is no __getnewargs__( ), an empty tuple is assumed.
Gabriel's solution using __getnewargs__ assures that args receives a
non-empty tuple. Your solution renders the empty tuple impotent by
specifying default values. Correct me if I am wrong.
I would be interested in a translation into English of the following
statement from the same reference that Gabriel provided:
Implementing this method [i.e., __getnewargs__] is needed if the
type establishes some internal invariants when the instance is
created, or if the memory allocation is affected by the values
passed to the __new__() method for the type (as it is for tuples
and strings).
What is an "internal invariant"? How do I know when the memory allocation
is affected? Does my Test class affect the memory allocation?
As a sidenote, your class works fine without changing anything when
pickling/unpickling instead of copying, although pickle calls
__reduce_ex__ too:
from pickle import dumps,loads
t = Test(0, 0)
assert loads(dumps(t)) == t
Perhaps someone more knowledgeable can explain the subtle differences
between pickling and copying here.
I have a situation in the full program where pickling seems to be failing in
the same manner as copy, but I have not been able yet to reproduce the
problem in a simple test program.
Thanks to all for your comments.
--
Jeffrey Barish This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: August1 |
last post by:
I've written an interface and implementation file along with a client
source file that allows the use of an overloaded subtraction operator.
However, when
using the program, I'm running into a memory problem that I'm not
readily seeing that lies within the overloaded operator - I think
pertaining to the temporary class object that is created and used to
return a value of the operands and the pointer variable of the class.
Could someone...
|
by: Tony Johansson |
last post by:
Hello Experts!!
I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.
Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this...
|
by: Lokkju |
last post by:
I am pretty much lost here - I am trying to create a managed c++
wrapper for this dll, so that I can use it from c#/vb.net, however, it
does not conform to any standard style of coding I have seen. It is
almost like it is trying to implement it's own COM interfaces...
below is the header, and a link to the dll+code:
Zip file with header, example, and DLL:...
|
by: Brian |
last post by:
Dear Programmers,
I have a class with a pointer to an array. In the destructor, I just
freed this pointer. A problem happens if I define a reference to a
vector of this kind of class. The destruction of the assigned memory
seems to call the class destructor more than once. I don't know the
reason or whether I used the vector class correctly. Attached is my
program. Thanks for your help.
Regards,
|
by: dragoncoder |
last post by:
Hello experts,
I have the following code me.
=cat mystring.h
#include<iostream>
using namespace std;
class mystring
{
| |
by: David |
last post by:
Hi all,
I posted my question two days ago, and tried to solve this problem.
but until now I didn't solve that. and I cut my codes so maybe this
time it is more readable.
/////////////////////////////////////////////////////
#ifndef MYCLASS_H_
#define MYCLASS_H_
|
by: Jeroen |
last post by:
Hi all,
I'm trying to implement a certain class but I have problems regarding
the copy ctor. I'll try to explain this as good as possible and show
what I tried thusfar. Because it's not about a certain code syntax but
more a 'code architecture' thing , I'll use simple example classes
(which are certainly not complete or working...) just to illustrate the
idea (and I may make some mistakes because I'm not that experienced...).
The...
|
by: saxman |
last post by:
Hi everyone,
I'm writing a class that inherits from std::vector in order to add
some additional functionality. I'm having a compiler problem, however,
when I have a function that returns this class. I've drilled down and
been able to create a very simple example of the problem. Here's my
code:
<code>
#include <vector>
|
by: Jun |
last post by:
Hello,
I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);
|
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: 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: 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...
|
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...
|
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: 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
| |