Reputation: 23
I am developing a user-centric front for an existing datastore.
Rather than having cumbersome lookup tables in my UI, I have attached UI "hints" to many of my data wrappers.
For instance:
class LibraryBook(IDatabaseItem):
"""There are a billion books in my library"""
@property
def name_hint(self):
"""This is a METHOD, I do not want to duplicate the fields in a new string!"""
return self.author + " " + self.title
@staticmethod
@property
def type_name_hint():
"""This is CONSTANT, there is no point in every instance having an attribute!"""
return "Book"
. . .
(The interface IDatabaseItem
is just to make code-completion from an IDE easier, I understand it isn't necessary in Python).
My concern is that all of these small methods are creating memory overhead. C++ would create a simple pointer to a v-table, but from what I have read Python uses a dict
, does this incur a massive memory overhead, not to mention having a dict-lookup to access otherwise trivial functions - for instance type_name_hint
above is essentially const.
So my question is: Is there a memory overhead, what is the better way if so, or how does Python resolve the issue if not.
Upvotes: 0
Views: 122
Reputation: 9597
Python class instances are basically a dict of instance variables, plus a reference to the class itself. Methods defined in the class do not affect the instance size AT ALL: they are found indirectly through the class reference. Basically, any attribute is looked up first in the instance's dict, then the class's dict, then the superclass's dict, and so on up the inheritance chain.
Upvotes: 2