1

I have a question about classes in Python. I have create a class that looks something like this:

class Model(Object):

    __table__ = 'table_name'

    def func():

    def func2():

The table name is a global variable that the functions feed off of. I am trying to add old data so I would like to change the table name depending on if I am filling in from old information, so this is what I tried.

class Model(Object):

    def __init__(self, backfill = False):
         self.backfill = backfill

    if self.backfill:
         __table__ = 'table_name'
    else:
         __table__ = 'table_name2'

    def func():

    def func2():

The final call would be something like this:

if backfill:
     model = Model(True).func()
else:
     model = Model.func()

Then I realized this wouldn't work because I cannot call self.backfill from outside the class definitions. I even tried creating a definition and calling that inside the class but that did not work either.

My question is:

  1. Is there a way to initialize the global variable inside the class from the class variables? Or is there a better way to do this in general?
zvone
  • 18,045
  • 3
  • 49
  • 77
RDizzl3
  • 846
  • 8
  • 18
  • *The table name is a global variable that the functions feed off of* - can you clarify what you mean by 'feed off of'? – Andrew Li Oct 01 '16 at 00:09
  • Have you looked at http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – perennial_ Oct 01 '16 at 00:09
  • `__table__` is not a *global variable*. It is an attribute of class `Model`. – zvone Oct 01 '16 at 00:17
  • `if self.backfill` is wrong because there is not `self` there. What are you actually trying to do? There is no theoretical way in which a class attribute (or a global variable for that matter) can depend on an instance (i.e. self) because there can be multiple instances, but there is only one class. – zvone Oct 01 '16 at 00:20

1 Answers1

2

__table__ is a class variabe, which means you can access it from an instance or from the class itself. You can update any instance value according to the value of backfill in the __init__ method:

class Model(Object):
    __table__ = 'table_name'
    def __init__(self, backfill = False):
        self.backfill = backfill
        if self.backfill:
            self.__table__ = 'table_name2'

Then to create an instance, just give the backfill parameter to the constructor, without any if/else statement:

print(Model.__table__)
# table_name
backfill = True
model = Model(backfill)
print(model.__table__)
# table_name2

But I don't see the point of using a class variable in your case. You can just define it in the __init__ method:

class Model(Object):
    def __init__(self, backfill = False):
        self.backfill = backfill
        self.__table__ = 'table_name'
        if self.backfill:
            self.__table__ = 'table_name2'
Frodon
  • 3,684
  • 1
  • 16
  • 33
  • Thank you Frodon! This is exactly what I was looking for - I am new to object oriented programming so couldn't articulate what exactly it was that I wanted but you were able to get around that! Thanks! – RDizzl3 Oct 13 '16 at 20:10