-2

i have a Sheet class which contains two attributes and each is a class

  1. summary (class)
  2. data (class)

summary and data has 1 attribute,df, which is a dataframe

my question is, am i right to say that since summary and data are classes:

  1. I shouldn't store summary and data directly as dataframes in Sheet class's attribute.

  2. I should store summary and data as objects and when main class wish to set summary/ data, i use Sheet's setter which uses summary/ data's setter to set the actual dataframe

Create sheet object in Main Class :

import Sheet
sheet = Sheet.Sheet() # create empty sheet
sheet.set_summary(new_df) # set summary
sheet.set_data(new_df) # set data

In Sheet Class :

import Summary
import Data

class Sheet:
    def __init__(self):
        self.name = None
        self._summary = Summary.Summary()
        self._data = Data.Data()

    def get_SheetSummary(self):
        return self._summary.get_summary()

    def set_SheetSummary(self,new_df):
        self._summary.set_summary(new_df)

    def get_SheetData(self):
        return self._data.get_data()

    def set_SheetData(self, new_df):
        self._data.set_data(new_df)
martineau
  • 119,623
  • 25
  • 170
  • 301
DJ_
  • 237
  • 1
  • 2
  • 8
  • Can you confirm whether an **instance** of Sheet class contains 2 classes or 2 instances of Summary and Data classes. A class and and instance of the classes are indeed related but are different animals. – Serge Ballesta May 13 '19 at 07:57
  • @SergeBallesta i think Sheet should contain two classes of Summary and Data class – DJ_ May 13 '19 at 08:13
  • I cannot understand what a *class of Summary class* is, sorry. There are different names for classes and instances for a reason. Python has even a notion of metaclass as an advanced feature when you have to process classes as if they were objects. But please be cautious and use the right terms if you want to be clear. – Serge Ballesta May 13 '19 at 08:23

1 Answers1

1

If Summary and Data are classes which only have a dataframe as attribute and don't handle anything extra, I don't see a reason to create an extra class for them instead of holding the dataframes directly in your Sheet class. If things get more complicated you can still extract those attributes into separate classes.

If both classes do something more than just holding the dataframes then you're probably on a good way to have them capsulated in a class.

Björn Böing
  • 1,662
  • 15
  • 23
  • data and summary class does different things including functions to manipulate their dataframe respectively. in this case i should keep them this way ? – DJ_ May 13 '19 at 08:11
  • Python doesn't encourage getters and setters like a number of others do. Instead you should just access the attributes directly if you need to manipulate them. If changing one affects the other, then the `Sheet` class should have methods which ensure that it happens properly. A container class generally should be responsible for forwarding calls to its contents. – martineau May 13 '19 at 08:35
  • @DJ_ Have a look at the comment from @martineau. It is toally correct what he writes. Keep your `Sheet` class and make sure its methods forward all required information to your other classes and simply change its fields directly whenever possible instead of using getter and setter methods. – Björn Böing May 13 '19 at 08:40