'Pass variable to tables2?

In trying to follow the DRY principle for python, I am attempting to define some information about a column in tables2 with a variable. I can see that the variable is being passed to my init function, but I can't see it in the main body of my class. Is there a better way to do this? Below is a simplified version of the table that I am trying to use.

class StuffTable(tables.Table):
  input(self.FontClass) # the variable is not showing up here
  columnAttributes = {"style": "font-size: 12px; max-width:120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"}
  columnHeaderAttributes = {"class": f"mb3 { self.FontClass }"}
  columnFooterAttributes = {"style": "font-size: 12px; font-weight: bold; max-width:120px; overflow: hidden; text-overflow: ellipsis;"}
  Type = tables.Column(verbose_name="", attrs={"th": columnHeaderAttributes, "td": columnAttributes})
  TypeDescription = tables.Column(verbose_name="Type", attrs={"th": columnHeaderAttributes, "td": columnAttributes})
  Column3= tables.Column(attrs={"th": columnHeaderAttributes, "td": columnAttributes})
  Column4= tables.Column(attrs={"th": columnHeaderAttributes, "td": columnAttributes})

  def __init__(self, *args, **kwargs):
      temp_fontclass = kwargs.pop("FontClass")
      super(StuffTable, self).__init__(*args, **kwargs)
      self.FontClass = temp_fontclass
      input(self.FontClass) # this does show that the variable is being passed


Solution 1:[1]

It won't show up here:

class StuffTable(tables.Table):
  input(self.FontClass) # the variable is not showing up here
  columnAttributes = {"style": "font-size: 12px; max-width:120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"}
  ...

The body of a class is defining class variables which get copied as instance variables by __init__. A tables2 instance ought to have a self.FontClass after instantiation, courtesy of your init. Try it in the shell.

>>> foo = StuffTable( FontClass='bar', whatever_keeps_it_happy)
>>> foo.FontClass

should show "bar". You might define a default FontClass = 'default' in your class body, and use kwargs.pop('FontClass', None) to make it optional at instantiation time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nigel222