还是__new__地狱...... [英] Still the __new__ hell ...

查看:55
本文介绍了还是__new__地狱......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉在这里提出太多关于__init__ __new__

的问题,但我总是在使用它们时发现了一个新问题。

我搜索过简单的__new__文档如何做

基本的东西,但没有找到。


尝试解决后,人们轻轻地贴在这里

(谢谢)我跑当我进一步开发时会遇到新的麻烦

开发。


这是新情况。


建议在上一篇文章中,我使用__new__来

子类日期类但现在cPickle / pickle加载

不起作用。


从datetime导入日期

导入cPickle,string


class MyDate(日期):

def __new __(cls,年,月=无,天=无):

如果类型(年份)是str:

年,月,日= map(int,string.split(年,'') - ''))

如果年份< 100:

年+ = 2000

返回日期.__新__(cls,年,月,日)


C1级(目标t):

def __init __(自我):

self.x = MyDate(" 2007-3-15")


def f(self):

print self.x


c1 = C1()


d = cPickle.dumps(c1)

c2 = cPickle.loads(d)

c2.f()


1.年作为一个字符串被传递给__new__但带有无效的

内容!


2.如果我在C1类中有一个__new__,cPickle.loads调用它

只有一个参数。这迫使__new__接受

只有1个参数(cls),我不想要正常的

情况。


感谢您的耐心等待。

Paulo

解决方案

3月17日9:31 pm,Paulo da Silva< ; psdasil ... @ esotericaX.ptXwrote:


很抱歉在这里提出太多关于__init__ __new__

的问题,但我总是找到一个使用它们时遇到的新问题。

我已经搜索过简单的__new__文档,了解如何做基本的东西但是找不到。


尝试解决方案后,人们轻轻地张贴在这里

(谢谢)当我进一步开发时,我遇到了新的麻烦

开发。


这是新的情况。


正如前一篇文章中所建议的那样,我使用了__new__来获得
子类日期类,但现在cPickle / pickle加载

不起作用。


来自datetime import d吃了

导入cPickle,字符串


类MyDate(日期):

def __new __(cls,年,月=无, day = None):

如果类型(年份)是str:

年,月,日= map(int,string.split(年,'' - '' ))

如果年份< 100:

年+ = 2000

返回日期.__新__(cls,年,月,日)

类C1(对象):

def __init __(自我):

self.x = MyDate(" 2007-3-15" ;)


def f(个体经营):

打印self.x


c1 = C1()


d = cPickle.dumps(c1)

c2 = cPickle.loads(d)

c2.f()



我没有尝试过你的代码,但我认为你可能需要在你的MyDate类中定义一个

__reduce__方法来给出一个关于如何挑选其实例的

python的线索。有关详细信息,请参阅:

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


类似于:


class MyDate(date):

...

def __reduce __(自我):

返回类型(self),(self.year,self.month,self.day)


可以解决你的问题。


HTH


-

Arnaud


3月17日下午4:31,Paulo da Silva< psdasil ... @ esotericaX.ptXwrote:


很抱歉在这里提出太多关于__init__ __new__

的问题,但我总是在使用它们时发现了一个新问题。

我有搜索了简单的__new__文档关于如何做基本的东西但没有找到的东西。


尝试解决方案后,人们轻轻地贴在这里

(谢谢)当我进一步使用时,我遇到了新的麻烦

developm这是新的情况。


正如上一篇文章中所建议的那样,我使用了__new__来

子类日期类,但现在cPickle / pickle加载

不起作用。


从datetime导入日期

import cPickle,string


等级MyDate(日期):

def __new __(cls,年,月=无,天=无):

如果类型(年份)是str:

年,月,日= map(int,string.split(年,'' - ''))

如果年份< 100:

年+ = 2000

返回日期.__新__(cls,年,月,日)


类C1(对象):

def __init __(自我):

self.x = MyDate(" 2007-3-15")


def f(个体经营):

打印self.x


c1 = C1()


d = cPickle .dumps(c1)

c2 = cPickle.loads(d)

c2.f( )


1.年份作为字符串传递给__new__但带有无效的

内容!


2。如果我在C1类中有一个__new__,cPickle.loads只用一个参数调用它。

。这迫使__new__接受

只有1个参数(cls),我不想要正常的

情况。


感谢您的耐心等待。

Paulo



您必须使用__new__作为此工厂方法的特殊原因吗?

这个版本有效,没有__new__的问题:


def getDate(* args):

if isinstance(args [0],basestring) :

年,月,日= map(int,string.split(args [0],'' - ''))

else:

年,月,日= args

如果年份< 100:

年+ = 2000

返回日期(年,月,日)


类C1(对象):

def __init __(自我):

#self.x = MyDate(" 2007-3-15")

self.x = getDate (2007-3-15)


def f(个体经营):

打印self.x

- Paul


Arnaud Delobelle escreveu:


3月17日,9:31 pm,Paulo da Silva< psdasil ... @ esotericaX.ptXwrote:



....


>我使用了__new__来继承日期类,但是现在cPickle / pickle加载
不起作用。

来自datetime导入日期
import cPickle,string

类MyDate(日期):
def __new __(cls,year,month = None,day = None):
如果type(year)是str:
年,月,日= map(int,string.split(年,'' - ''))
如果年份< 100:
年份= = 2000
返回日期.__新__(cls,年,月,日)

类C1(对象):
def __init __(自我):
self.x = MyDate(" 2007-3-15")

def f(self):
print self.x

c1 = C1()

d = cPickle.dumps(c1)
c2 = cPickle.loads(d)
c2.f()



我没有尝试过你的代码,但我认为你可能需要在你的MyDate类中定义一个

__reduce__方法,以便给出一个线索。

python关于如何挑选它的实例。有关详细信息,请参阅:

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


类似于:


class MyDate(date):

...

def __reduce __(自我):

返回类型(self),(self.year,self.month,self.day)


可以解决你的问题。


HTH


-

Arnaud



谢谢。这与你写的方式完全一致。

然而我误解了一些东西。不能腌制看到那是什么?b $ b MyDate派生自日期还需要查看日期变量?

我什么时候需要这样做?我正在使用更复杂的泡菜

课程没有这个问题。


谢谢

Paulo


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,year,month=None,day=None):
if type(year) is str:
year,month,day=map(int,string.split(year,''-''))
if year<100:
year+=2000
return date.__new__(cls,year,month,day)

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.loads(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

解决方案

On Mar 17, 9:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:

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,year,month=None,day=None):
if type(year) is str:
year,month,day=map(int,string.split(year,''-''))
if year<100:
year+=2000
return date.__new__(cls,year,month,day)

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.loads(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


On Mar 17, 4:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:

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,year,month=None,day=None):
if type(year) is str:
year,month,day=map(int,string.split(year,''-''))
if year<100:
year+=2000
return date.__new__(cls,year,month,day)

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.loads(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


Arnaud Delobelle escreveu:

On Mar 17, 9:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:

....

>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,year,month=None,day=None):
if type(year) is str:
year,month,day=map(int,string.split(year,''-''))
if year<100:
year+=2000
return date.__new__(cls,year,month,day)

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.loads(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 misunderstanding 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


这篇关于还是__new__地狱......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆