1

I'm sure this is something trivial, but I can't seem to find any insight from the Python (3) docs.

Given the following code, how can I ensure that the t class attribute in the base class is updated in the inherited class.

class DbBase:
    table = None
    t = table

class Articles(DbBase):
    table = Table(....)

I'd now like to be able to refer to Article.table as Article.t as well.

Thanks!

cgons
  • 1,301
  • 1
  • 8
  • 5

1 Answers1

4

Make t a property:

class DbBase:
    table = None
    @property
    def t(self):
        return self.table

class Articles(DbBase):
    table = {} # for demo purposes

A = Articles()
A.t # returns {}
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 2
    Probably want a setter on `t` too. – kindall Aug 08 '14 at 16:47
  • https://docs.python.org/3.4/library/functions.html#property -- Thanks! Didn't realize that @property was applicable to class attributes. Related SO question with more insights: http://stackoverflow.com/questions/7374748/whats-the-difference-between-a-python-property-and-attribute – cgons Aug 08 '14 at 17:51