I have a class A and i want a class B with exactly the same capabilities. I cannot or do not want to inherit from B, such as doing class B(A):pass Still i want B to be identical to A, yet have a different i: id(A) != id(B) Watch out, i am not talking about instances but classes to be cloned.
+7
A:
I'm pretty sure whatever you are trying to do can be solved in a better way, but here is something that gives you a clone of the class with a new id:
def c():
class Clone(object):
pass
return Clone
c1 = c()
c2 = c()
print id(c1)
print id(c2)
gives:
4303713312
4303831072
truppo
2009-12-19 18:52:37
This is exactly what i was looking for. The solution is perfect: simple and to the point.I knew there was a solution but i knew it had to be simple.This is the first time i ask a question to SO and i am amazed how reactive it is, compared to the almost defunct comp.lanp.python.Thanks a lot!
Francois
2009-12-19 19:09:46
that doesn't take an existing class though. Seems like you really wanted a "class generator" that generates the same class everytime.
zzzeek
2009-12-19 19:15:30
@Francois: Why did you say in your question "I cannot or do not want to inherit from B, such as doing class B(A):pass" if you now accept this solution?
Alex
2009-12-19 19:26:30
@Alex. There is no inheritance involved in the solution. I wrapped the original class A in a factory function:def makeTree():
Francois
2009-12-19 19:32:25
@Alex. There is no inheritance involved in the solution. I wrapped the original class A in a factory function:def makeTree(): class A(...): my original code return AB=makeTree()
Francois
2009-12-19 19:33:35
I messed up with the indentation. Third attempt. @Alex. There is no inheritance involved in the solution. I wrapped the original class A in a factory function: def makeTree(): class A:....return A. <br>Then i do: B=makeTree()
Francois
2009-12-19 19:35:46
@Francois, comments do not preserve formatting. And you can't edit them or delete them later. Since you are the original poster you can edit your question, and when you write an answer you can edit the answer; and questions and answers do preserve formatting. The only formatting that really works in a comment is to use the backward single quote to indicate "code", but it will not work well if you try to enter multi-line code.
steveha
2009-12-20 07:34:16
@steveha, you can delete comments. I'm not sure if it requires some minimum reputation count, but you and I can certainly delete our own, though maybe Francois needs to build more rep to do the same.
Peter Hansen
2009-12-20 15:34:17
A:
You can clone the class via inheritance. Otherwise you are just passing around a reference to the class itself (rather than a reference to an instance of the class). Why would you want to duplicate the class anyway? It's obvious why you would want to create multiple instances of the class, but I can't fathom why you would want a duplicate class. Also, you could simply copy and paste with a new class name...
Dustin
2009-12-19 18:54:02
A:
maybe i misunderstood you question but what about wrapping A in B?
class A:
def foo(self):
print "A.foo"
class B:
def __init__(self):
self._i = A()
def __getattr__(self, n):
return getattr(self._i, n)
mg
2009-12-19 19:09:14
+3
A:
I guess this is not what you wanted but its what the question seems to be asking for...
class Foo(object):
def bar(self):
return "BAR!"
cls = type("Bar", (object,), dict(Foo.__dict__))
print cls
x = cls()
print x.bar()
zzzeek
2009-12-19 19:12:10
This is the answer to the question I thought he was asking. Users might want to deep copy the Foo.__dict__, depending on their usage. Could you wrap it in a reusable function (where you'd pass old class and name of new)?
Mark Santesson
2009-12-19 19:40:36
I wonder why I've always used `type.__new__()` here ? I first saw it in a particular Python package written by a well-known Python guru and he was using `__new__()` for some reason.
zzzeek
2009-12-19 23:44:26