python: making your own interned type

Status
You're currently viewing only bk0's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

bk0

Ars Tribunus Militum
1,801
[url=http:/civis/viewtopic.php?p=26425715#p26425715:3cx197lq said:
DavlinLotze[/url]":3cx197lq]
[url=http:/civis/viewtopic.php?p=26414851#p26414851:3cx197lq said:
bk0[/url]":3cx197lq]My first impression is that you are overthinking memory management--this is Python, focus on the algorithms and let memory do what it does unless you hit an obvious constraint--but it's hard to tell without a specific example.

Well, if I could ignore it at this phase, I would, but there's two problems:
1) If there's some way I can arrange things at an earlier stage - without the recycling implemented - that would prevent me from having to rewrite a bunch of code and interfaces later, I'd like to know.
1a) (Optimally, I'd like a system where I can just turn the interning on or off, or somewhere in between with just a change of a few variables - that would allow me to compare CPU/memory usage patterns, or tune it according to a particular usage case.)
2) I foresee my program depending on interning for more than just reducing memory footprint. It would allow me to make changes to just one object and have it take effect broadly instead of having to walk/search some huge complex tree and change each matching object.


[url=http:/civis/viewtopic.php?p=26414851#p26414851:3cx197lq said:
bk0[/url]":3cx197lq]Define "semantically equivalent" (preferably with pseudocode).

For any two instances a and b of class C, a == b if and only if a.some_core_attribute == b.some_core_attribute. (And, of course a == b if a is b or if a.some_core_attribute is b.some_core_attribute; these are a quicker check, since some_core_attribute may be complex.)

While, for the bulk of the processing, having one copy each of an instance of C is good enough, I foresee situations where it would be handy to have separate copies of them in different segments of the program - each copy with different extended attributes.

Class properties?

Code:
class DataObject:
    invariant_property = "yo"

    def __init__(self, variant_property):
        self.variant_property = variant_property


a = DataObject("Spam")
b = DataObject("Eggs")

print(a.invariant_property)  # "yo"
print(a.variant_property)    # "Spam"
print(b.invariant_property)  # "yo"
print(b.variant_property)    # "Eggs"

DataObject.invariant_property = "holy crap"

print(a.invariant_property)  # "holy crap"
print(b.invariant_property)  # "holy crap"
 

bk0

Ars Tribunus Militum
1,801
If the class objects are modeling the same underlying data structure, they should all be in the same model. If that isn't the case and you want to use child objects in an efficient way you can easily use references, which you are using in your above code sample (perhaps unknowingly). Python nearly always gives you reference semantics (where are you getting copy_of_alpha from, how do you know it's a copy)?

Code:
>>> bar = { 'Spam': 1 }
>>> class Foo:
...     def __init__(self, val):
...             self.val = val
... 
>>> f = Foo(bar)
>>> bar['Eggs'] = 2
>>> f.val
{'Eggs': 2, 'Spam': 1}

Depending on what you're trying to do, does it even make sense to keep the whole object tree in memory at the same time? If this was a typical web app you would probably have a data store. In that case you'd want to store a reference to the child via foreign key or DBRef or whatever and let the data layer figure it out.
 
Status
You're currently viewing only bk0's posts. Click here to go back to viewing the entire thread.
Not open for further replies.