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?
4
  • The table name is a global variable that the functions feed off of - can you clarify what you mean by 'feed off of'? Commented Oct 1, 2016 at 0:09
  • Have you looked at stackoverflow.com/questions/423379/… Commented Oct 1, 2016 at 0:09
  • __table__ is not a global variable. It is an attribute of class Model. Commented Oct 1, 2016 at 0: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. Commented Oct 1, 2016 at 0:20

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.