0

I'm working a special situation where I'm trying to emulate the django feel of model classes like so:

class packet(models.packet):
    field1 = models.IntField()
    field2 = models.IntField()

There's a lot of background interfacing using metaclassing but the over idea is to allow the user to interact with the fields like so:

p = packet()
p.field1 = 12
p.field1 == 12 # true

while still not compromising the field type:

isinstance(p.field1, models.IntField) # true

The problem that I'm facing is that two packet objects share the same Fields since they're class properties:

p1 = packet()
p2 = packet()

p1.field1 = 12
p2.field2 = 14

p1 is p2 # false
p1.field1 is p2.field1 # true

How do I instantiate a new property object for each new parent object?

To give better context feel free to browse the source here

James Mertz
  • 8,459
  • 11
  • 60
  • 87
  • I've updated to the question to hopefully reflect the bigger problem that I'm facing and therefore hopefully still not a duplicate of https://stackoverflow.com/questions/1680528/how-to-avoid-having-class-data-shared-among-instances – James Mertz Nov 14 '17 at 20:32
  • You're trying to have both `p.field1 == 12` *and* `isinstance(p.field1, models.IntField)`? Don't try it; you'll end up in a horrible world of non-transitive `==` and 3 dozen operator overloads and a dozen entries on your "known issues" list that will never go away. – user2357112 Nov 21 '17 at 20:57

0 Answers0