1

I have the following problem and I need advice on how to solve it the best technically in Python. As I am new to programming I would like to have some advice.

So I will have the following object and they should store something. Here is an example:

  1. object 1: cash dividends (they will have the following properties)

    • exdate (will store a list of dates)
    • recorddate (will store a list of dates)
    • paydate (will store a list of dates)
    • ISIN (will store a list of text)
  2. object 2: stocksplits (they will have the following prpoerties)

    • stockplitratio (will be some ration)
    • exdate(list of dates)
    • ...

I have tried to solve it like this:

class cashDividends(object):

    def __init__(self, _gross,_net,_ISIN, _paydate, _exdate, _recorddate, _frequency, _type, _announceddate, _currency):
        self.gross = _gross
        self.net = _net
        self.ISIN = _ISIN
        self.paydate = _paydate
        self.exdate = _exdate
        self.recorddate = _recorddate
        self.frequency = _frequency
        self.type = _type
        self.announceddate = _announceddate
        self.currency = _currency

So if I have this I would have to create another class named stockplits and then define an __init__ function again.

However is there a way where I can have one class like "Corporate Actions" and then have stock splits and cashdividends in there ?

2
  • 1
    I think python supports inner classes (class within a class) Commented Feb 5, 2016 at 14:37
  • As you said you can make a class that has an __init__ that only sets up required parameters. Then define additional methods to preform or store other tasks. Look at the first figure here. Eventually you may want to make a master class that defines common methods, and subclasses that do specific work. That concept is called inheritance which is described at that link as well. Commented Feb 5, 2016 at 14:38

3 Answers 3

2

Sure you can! In python you can pass classes to other classes. Here a simple example:

class A():
    def __init__(self):
        self.x = 0

        
class B():
    def __init__(self):
        self.x = 1

        
class Container():
    def __init__(self, objects):
        self.x = [obj.x for obj in objects]
        
a = A()
b = B()
c = Container([a,b])

c.x

[0,1]
Sign up to request clarification or add additional context in comments.

3 Comments

what I actually want to call in my main.py then is the following: mycashdiv = cashdiv() mycashdiv.exdate =[bla,bal] mycashdiv.recorddate =[bla,bla] mystocksplit = stockplit() mystocksplit.newshares=[3,2,3] ... ... >>
@Nant I did not get your last comment.. can you explain it?
So essentially what I want users to call in any python file is the following: cashdividend =(enter your cash div parameters) stocksplits = (enter your stock split parameters) like we normally initiate integers or strings. I just want to have my own objects like cash dividends and the question was how can I create one class for this
1

If I understood correctly what you want is an object that has other objects from a class you created as property?

class CorporateActions(object):
    def __init__(self, aCashDividend, aStockSplit):
        self.cashDividend = aCashDividend
        self.stockSplit = aStockSplit


myCashDividends = CashDividends(...) #corresponding parameters here
myStockSplit = StockSplit(...)
myCorporateActions = CorporateActions(myCashDividends, myStockSplit)

Comments

1

Strictly speaking this answer isn't an answer for the final question. However, it is a way to make your life slightly easier.

Consider creating a sort-of template class (I'm using this term loosely; there's no such thing in Python) that does the __init__ work for you. Like this:

class KwargAttrs():
    def __init__(self, **kwargs):
        for k,v in kwargs.items():
            setattr(self, k, v)
    def _update(self, **kwargs):
        args_dict = {k:(kwargs[k] if k in kwargs else self.__dict__[k]) for k in self.__dict__} 
        self.__dict__.update(args_dict)

This class uses every supplied keyword argument as an object attribute. Use it this way:

class CashDividends(KwargAttrs):
    def __init__(self, gross, net, ISIN, paydate, exdate, recorddate, frequency, type, announceddate, currency):
        # save the namespace before it gets polluted
        super().__init__(**locals()) 

        # work that might pollute local namespace goes here

        # OPTIONAL: update the argument values in case they were modified: 
        super()._update(**locals())

Using a method like this, you don't have to go through the argument list and assign every single object attribute; it happens automatically.

We bookend everything you need to accomplish in the __init__ method with method calls to the parent-class via super(). We do this because locals() returns a dict every variable in the function's current namespace, so you need to 1.) capture that namespace before any other work pollutes it and 2.) update the namespace in case any work changes the argument values.

The call to update is optional, but the values of the supplied arguments will not be updated if something is done to them after the call to super().__init__() (that is, unless you change the values using setattr(self, 'argname, value)`, which is not a bad idea).

You can continue using this class like so:

class StockSplits(KwargAttrs):
    def __init__(self, stocksplitratio, gross, net, ISIN, paydate, exdate, recorddate, frequency, type, announceddate, currency):
        super().__init__(**locals())

As mentioned in the other answers you can create a container for our other classes, but you can even do that using this same template class:

class CorporateActions(KwargAttrs):
    def __init__(self, stock_splits , cash_dividends):
        super().__init__(**locals())

ca = CorporateActions(stock_splits = StockSplits(<arguments>), cash_dividends = CashDividends(<arguments>) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.