473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Still the __new__ hell ...

Sorry to put here too many questions about __init__ __new__
stuff but I always found a new problem when using them.
I have searched for simple __new__ docs on how to do the
basic things but find none.

After trying the solutions people gently posted here
(thanks) I run into new trouble when I go with further
development.

Here is the new situation.

As suggested in a previous post, I used __new__ to
subclass date class but now cPickle/pickle loads
does not work.

from datetime import date
import cPickle,string

class MyDate(date):
def __new__(cls,yea r,month=None,da y=None):
if type(year) is str:
year,month,day= map(int,string. split(year,'-'))
if year<100:
year+=2000
return date.__new__(cl s,year,month,da y)

class C1(object):
def __init__(self):
self.x=MyDate(" 2007-3-15")

def f(self):
print self.x

c1=C1()

d=cPickle.dumps (c1)
c2=cPickle.load s(d)
c2.f()

1. year is passed to __new__ as a string but with invalid
contents!

2. If I had a __new__ in class C1, cPickle.loads invokes it
with only one parameter. This forces to allow __new__ accept
only 1 parameter (cls) which I don't want for the normal
situation.

Thanks for your patience.
Paulo
Mar 17 '07 #1
18 1703
On Mar 17, 9:31 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
Sorry to put here too many questions about __init__ __new__
stuff but I always found a new problem when using them.
I have searched for simple __new__ docs on how to do the
basic things but find none.

After trying the solutions people gently posted here
(thanks) I run into new trouble when I go with further
development.

Here is the new situation.

As suggested in a previous post, I used __new__ to
subclass date class but now cPickle/pickle loads
does not work.

from datetime import date
import cPickle,string

class MyDate(date):
def __new__(cls,yea r,month=None,da y=None):
if type(year) is str:
year,month,day= map(int,string. split(year,'-'))
if year<100:
year+=2000
return date.__new__(cl s,year,month,da y)

class C1(object):
def __init__(self):
self.x=MyDate(" 2007-3-15")

def f(self):
print self.x

c1=C1()

d=cPickle.dumps (c1)
c2=cPickle.load s(d)
c2.f()
I haven't tried your code but I think that you may need to define a
__reduce__ method in your MyDate class in order to give a clue to the
python as to how to pickle its instances. For more details see:

http://docs.python.org/lib/node321.html

Something like:

class MyDate(date):
...
def __reduce__(self ):
return type(self), (self.year, self.month, self.day)

might solve your problem.

HTH

--
Arnaud

Mar 17 '07 #2
On Mar 17, 4:31 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
Sorry to put here too many questions about __init__ __new__
stuff but I always found a new problem when using them.
I have searched for simple __new__ docs on how to do the
basic things but find none.

After trying the solutions people gently posted here
(thanks) I run into new trouble when I go with further
development.

Here is the new situation.

As suggested in a previous post, I used __new__ to
subclass date class but now cPickle/pickle loads
does not work.

from datetime import date
import cPickle,string

class MyDate(date):
def __new__(cls,yea r,month=None,da y=None):
if type(year) is str:
year,month,day= map(int,string. split(year,'-'))
if year<100:
year+=2000
return date.__new__(cl s,year,month,da y)

class C1(object):
def __init__(self):
self.x=MyDate(" 2007-3-15")

def f(self):
print self.x

c1=C1()

d=cPickle.dumps (c1)
c2=cPickle.load s(d)
c2.f()

1. year is passed to __new__ as a string but with invalid
contents!

2. If I had a __new__ in class C1, cPickle.loads invokes it
with only one parameter. This forces to allow __new__ accept
only 1 parameter (cls) which I don't want for the normal
situation.

Thanks for your patience.
Paulo
Any special reason you have to use __new__ for this factory method?
This version works, without the problems with __new__:

def getDate(*args):
if isinstance(args[0],basestring):
year,month,day = map(int,string. split(args[0],'-'))
else:
year,month,day = args
if year < 100:
year += 2000
return date(year,month ,day)

class C1(object):
def __init__(self):
#self.x=MyDate( "2007-3-15")
self.x = getDate("2007-3-15")

def f(self):
print self.x
-- Paul

Mar 17 '07 #3
Arnaud Delobelle escreveu:
On Mar 17, 9:31 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
....
>I used __new__ to
subclass date class but now cPickle/pickle loads
does not work.

from datetime import date
import cPickle,string

class MyDate(date):
def __new__(cls,yea r,month=None,da y=None):
if type(year) is str:
year,month,day= map(int,string. split(year,'-'))
if year<100:
year+=2000
return date.__new__(cl s,year,month,da y)

class C1(object):
def __init__(self):
self.x=MyDate(" 2007-3-15")

def f(self):
print self.x

c1=C1()

d=cPickle.dump s(c1)
c2=cPickle.loa ds(d)
c2.f()

I haven't tried your code but I think that you may need to define a
__reduce__ method in your MyDate class in order to give a clue to the
python as to how to pickle its instances. For more details see:

http://docs.python.org/lib/node321.html

Something like:

class MyDate(date):
...
def __reduce__(self ):
return type(self), (self.year, self.month, self.day)

might solve your problem.

HTH

--
Arnaud
Thanks. This works exactly the way you wrote.
Yet I am misunderstandin g something. Can't pickle "see" that being
MyDate derived from date it also has to look at variables from date?
When do I need to do this? I am using pickle with a lot more complex
classes without this problem.

Thank you
Paulo
Mar 18 '07 #4
On Mar 17, 11:48 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
Arnaud Delobelle escreveu:On Mar 17, 9:31 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
[snip]
Thanks. This works exactly the way you wrote.
Yet I am misunderstandin g something. Can't pickle "see" that being
MyDate derived from date it also has to look at variables from date?
When do I need to do this? I am using pickle with a lot more complex
classes without this problem.
Without the MyDate.__reduce __ method, Python uses the
datetime.date._ _reduce__ method to pickle your MyDate instances, then
it uses MyDate.__new__ to unpickle them. But your MyDate.__new__
method does not understand the format of a pickled date. You could
also change the MyDate.__new__ method so that it does understand it,
but it wouldn't be that easy as you want it to accept a string, and
this is the format that dates are pickled in.

--
Arnaud

Mar 18 '07 #5
Arnaud Delobelle escreveu:
On Mar 17, 11:48 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
>Arnaud Delobelle escreveu:On Mar 17, 9:31 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:

[snip]
>Thanks. This works exactly the way you wrote.
Yet I am misunderstandin g something. Can't pickle "see" that being
MyDate derived from date it also has to look at variables from date?
When do I need to do this? I am using pickle with a lot more complex
classes without this problem.

Without the MyDate.__reduce __ method, Python uses the
datetime.date._ _reduce__ method to pickle your MyDate instances, then
it uses MyDate.__new__ to unpickle them. But your MyDate.__new__
method does not understand the format of a pickled date. You could
also change the MyDate.__new__ method so that it does understand it,
but it wouldn't be that easy as you want it to accept a string, and
this is the format that dates are pickled in.

--
Arnaud
I see now. I need to read a little further about this stuff as soon as
I get some time to do it.

Thanks Arnaud.
Mar 18 '07 #6
Paulo da Silva a écrit :
(snip)

Not an answer to your question, just a couple advices:
from datetime import date
import cPickle,string
The string module is mostly deprecated. You should use str type methods
whenever possible (cf below)
class MyDate(date):
def __new__(cls,yea r,month=None,da y=None):
if type(year) is str:
And what if it's a unicode string ?
The correct idiom here is:
if isinstance(year , basestring):
year,month,day= map(int,string. split(year,'-'))
year, month, day = map(int, year.split('-'))
if year < 100:
year += 2000
return date.__new__(cl s,year,month,da y)
(snip)
Mar 19 '07 #7
Bruno Desthuilliers escreveu:
Paulo da Silva a écrit :
....
>class MyDate(date):
def __new__(cls,yea r,month=None,da y=None):
if type(year) is str:

And what if it's a unicode string ?
The correct idiom here is:
if isinstance(year , basestring):

Thanks.
If I do type(year) I get either int or str (may be unicode for unicode
strings) but never anything like basestring. As a relatively inexperient
in python, how could I know that a 'string' is an instance of
basestring? x=u"xxxxxx"; help(x) says this is unicode based on
basestring but help does not "work" for x="xxxxxxxx".
May be the python tutorial should be upgraded to include these new
concepts. Also covering the basics of __init__/__new__ (why have both?)
would be nice.

Paulo
Mar 19 '07 #8
Paulo da Silva wrote:
As a relatively inexperient
in python, how could I know that a 'string' is an instance of
basestring?
isinstance(x, basestring)

This works because basestring is defined as the
tuple (str, unicode) and isinstance accepts a
tuple of types as well as just a single type.

--
Greg
Mar 20 '07 #9
greg wrote:
Paulo da Silva wrote:
>As a relatively inexperient
in python, how could I know that a 'string' is an instance of
basestring?

isinstance(x, basestring)

This works because basestring is defined as the
tuple (str, unicode) and isinstance accepts a
tuple of types as well as just a single type.
The idea is right, but the detail is completely wrong.

basestring is a *type*.
>>basestring
<type 'basestring'>

It's the base class of which both str and unicode are subclasses.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Mar 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2038
by: H Jansen | last post by:
I try to work out how to use __new__ and metaclass (or __metaclass__) mechanisms in order to change the base class at runtime. I haven't been successful so far, however, even after also reading most of the relevant documentation. The most simple representation (an oversimplification indeed) of what I'm trying to accomplish is this: class Base1: pass
9
6217
by: Felix Wiemann | last post by:
Sometimes (but not always) the __new__ method of one of my classes returns an *existing* instance of the class. However, when it does that, the __init__ method of the existing instance is called nonetheless, so that the instance is initialized a second time. For example, please consider the following class (a singleton in this case): >>> class C(object): .... instance = None .... def __new__(cls): .... if C.instance is None:
5
2500
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in what situations we must use __new__? Can __new__ take the place of __init__? Thanks.
5
2157
by: Ken Schutte | last post by:
Hi, I'm been trying to create some custom classes derived from some of python's built-in types, like int and list, etc. I've run into some trouble, which I could explain with a couple simple examples. Lets say I want an int-derived class that is initilized to one greater than what it's constructor is given: class myint(int): def __new__(cls, intIn):
1
1200
by: Frank Benkstein | last post by:
Hi, the behaviour I always observed when creating instances by calling the class A is that '__init__' is always only called when the object returned by A.__new__ is an instance of A. This can be observed by the following code: class A(object): def __new__(cls, *args, **kwds): print "A.__new__", args, kwds
5
1469
by: Sandra-24 | last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to have more specific exceptions. I want to change S3ResponseError into about 30 more specific errors. Preferably I want to do this by changing as little code as possible. I also want the new exceptions to be a subclass of the old S3ResponseError so as to not break old code that catches it. If I meet these two requirements I expect my modification to make it into boto and then...
4
3543
by: Steven D'Aprano | last post by:
When you call a new-style class, the __new__ method is called with the user-supplied arguments, followed by the __init__ method with the same arguments. I would like to modify the arguments after the __new__ method is called but before the __init__ method, somewhat like this: .... def __new__(cls, *args): .... print "__new__", args
3
2770
by: Torsten Mohr | last post by:
Hi, i have some questions related to new style classes, they look quite useful but i wonder if somebody can give me an example on constructors using __new__ and on using __init__ ? I just see that when using it i can't give parameters to __new__ and when i additionally define __init__ then __new__ is not called.
3
1382
by: macaronikazoo | last post by:
i'm having a hell of a time getting this to work. basically I want to be able to instantiate an object using either a list, or a string, but the class inherits from list. if the class is instantiated with a string, then run a method over it to tokenize it in a meaningful way. so how come this doesn't work??? if I do this: a=TMP( 'some string' )
0
8863
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
8739
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,...
0
9384
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...
0
9238
jinu1996
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...
0
9088
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
4502
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
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
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.