__metaclass__ and deepcopy issue

W

Wouter DW

I read the article on http://www.python.org/download/releases/2.2/descrintro/#metaclasses
and started using autoprop.
But now I have a problem I can't seem to solve myself.

class autoprop(type):
def __init__(cls, name, bases, dict):
super(autoprop, cls).__init__(name, bases, dict)
props = {}
for name in dict.keys():
if name.startswith("_get_") or name.startswith("_set_"):
props[name[5:]] = 1
for name in props.keys():
fget = getattr(cls, "_get_%s" % name, None)
fset = getattr(cls, "_set_%s" % name, None)
setattr(cls, name, property(fget, fset))
class InvertedX:
__metaclass__ = autoprop
def _get_x(self):
return -self.__x
def _set_x(self, x):
self.__x = -x

a = InvertedX()
from copy import deepcopy
b = deepcopy(a)

The deepcopy gives an error inside copy.py of the python lib.

TypeError: descriptor '__reduce__' of 'object' object needs an
argument

Does anybody know how to solve this issue?
 
R

Raymond Hettinger

I read the article onhttp://www.python.org/download/releases/2.2/descrintro/#metaclasses
and started using autoprop.
But now I have a problem I can't seem to solve myself.

class autoprop(type):
  def __init__(cls, name, bases, dict):
    super(autoprop, cls).__init__(name, bases, dict)
    props = {}
    for name in dict.keys():
      if name.startswith("_get_") or name.startswith("_set_"):
        props[name[5:]] = 1
    for name in props.keys():
      fget = getattr(cls, "_get_%s" % name, None)
      fset = getattr(cls, "_set_%s" % name, None)
      setattr(cls, name, property(fget, fset))
class InvertedX:
  __metaclass__ = autoprop
  def _get_x(self):
    return -self.__x
  def _set_x(self, x):
    self.__x = -x

a = InvertedX()
from copy import deepcopy
b = deepcopy(a)

The deepcopy gives an error inside copy.py of the python lib.

Hmm, your code works just fine for me in Py2.6.
If I add "a.x = 10" before the deepcopy and
"print b.x" after the deepcopy, it also shows
the desired result.

TypeError: descriptor '__reduce__' of 'object' object needs an
argument

Does anybody know how to solve this issue?

Usually, when you get errors of this kind with copy, deepcopy, or
pickle, it means that the copier can't figure out how to extract
the relevant components and then re-assemble them into a new instance.

This means that you need to define some combination of __copy__,
__deepcopy__, __reduce__, __getstate__, __setstate__, __getinitargs__,
and/or __getnewargs__. See the pickle module and copy module for
all the gory details.

One other thought, class decorators can be much easier to use and
understand than an equivalent metaclass:

def autoprop(cls):
for name in cls.__dict__.keys():
if name.startswith("_get_") or name.startswith("_set_"):
name = name[5:]
fget = getattr(cls, "_get_%s" % name, None)
fset = getattr(cls, "_set_%s" % name, None)
setattr(cls, name, property(fget, fset))
return cls

@autoprop
class InvertedX:
def _get_x(self):
return -self.__x
def _set_x(self, x):
self.__x = -x

a = InvertedX()
a.x = 10
print a.x
from copy import deepcopy
b = deepcopy(a)
print b.x


Raymond


P.S. Minor nits in the original code. When you use a dict like
"props" but care only about the key, not the value, that usually means
that you're better-off with a set() than a dict(). Also, you don't
even need to build-up "props" since the values can be used immediately
(like in my example code). But the most important part is that
whenever you're using metaclasses for registration, validation, or
simple transformation (as in your example), then a simple class
decorator is often a better choice. If you don't have Py2.6 yet, then
you can get the same effect by defining the class and *then* writing,
"Inverted = autoprop(Inverted).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top