Featured post
sql - Python: How can I merge these 2 overloaded constructors into 1 constructor -
i have class called portfolio & right have 2 constructors (although discovered u cant overload functions in python) so have merge these 2 constructors.
i experienced python programmer merging 2 constructors pretty contradictory doing head in! :p
the first constructor:
+ purpose create .db file & initialise class variables
- initialises class member variables
- checks whether file path&name db_path exists: if exit coz dont want overwrite existing database return none
- create database (sql database) & set necessary tables
- import portfolio data (date created, etc)
the 2nd constructor:
+ purpose import stock data existing sql database file & initialise variables
- initialises class member variables
- checks whether file path&name db_path exists: if not exit coz dont want overwrite existing database return none
- call function import stocks database
- import portfolio data (date created, etc)
how can merge these 2 constructors, ugh, why doesn't python allow overloading
class portfolio: """ portfolio class implementation """ # class variables: # self.db_path # self.app_parent # self.tracking_stocks # self.registered_stocks # self.total_purchase_exp # self.ytd_roi # self.cash_reserve # class functions: def __init__( self, _app_parent, _db_path, stock_list ): """ constructor: """ self.db_path = _db_path self.app_parent = _app_parent self.tracking_stocks = [] self.registered_stocks = [] self.total_purchase_exp = 0 self.ytd_roi = 0 self.cash_reserve = 500 if database_exists( self.db_path ): return none self.create_database() self.import_portfolio_data() stock_data in stock_list: self.add_stock( stock_data ) def __init__( self, _app_parent, _db_path ): """ constructor: """ self.db_path = _db_path self.app_parent = _app_parent self.tracking_stocks = [] self.registered_stocks = [] self.total_purchase_exp = 0 self.ytd_roi = 0 self.cash_reserve = 500 if not self.database_exists( self.db_path ): return none self.import_portfolio_data() self.import_stocks()
why have happen in constructors?
you construct object, call separate methods on for importing stock lists, or creating database.
wrapping in builder pattern may suit existing design.
- Get link
- X
- Other Apps
Comments
Post a Comment