For official documentation on python classes visit: https://docs.python.org/3/tutorial/classes.html
Creating a class in python, similar to other languages, creates a new type of object which can allows for instances of that object to be made. Classes can define attributes and methods to be used during the lifecycle of that object. Python supports class inheritance, but we will not dive into that here.
A basic class definition is as below:
In [7]:
class Stock:
# the init function gets executed once the object Stock is instantiated
def __init__(self,ticker):
self.ticker = ticker
def return_ticker(self):
print(self.ticker)
APPL = Stock('APPL')
APPL.return_ticker()
You can store a function from a class as a variable and call it later.
In [10]:
rt = APPL.return_ticker
rt()
There are two type of variables within a class. A class variable is shared between all instances of that class.
In [12]:
class Stock:
base_request_url = 'https://financialmodelingprep.com/api/v3/'
# the init function gets executed once the object Stock is instantiated
def __init__(self,ticker):
self.ticker = ticker
def return_ticker(self):
print(self.ticker)
INTC = Stock('INTC')
print(INTC.base_request_url)
MSFT = Stock('MSFT')
print(MSFT.base_request_url)
In [ ]:
An instance variable is one that is specific to the instance, which has already been showed above.
In [13]:
class Stock:
base_request_url = 'https://financialmodelingprep.com/api/v3/'
# the init function gets executed once the object Stock is instantiated
def __init__(self,ticker):
self.ticker = ticker
def return_ticker(self):
print(self.ticker)
INTC = Stock('INTC')
print(INTC.ticker)
MSFT = Stock('MSFT')
print(MSFT.ticker)
If the same name occurs in both the instance and the class - the instance class wins.
In [14]:
class Stock:
base_request_url = 'https://financialmodelingprep.com/api/v3/'
ticker = 'APPL'
# the init function gets executed once the object Stock is instantiated
def __init__(self,ticker):
self.ticker = ticker
def return_ticker(self):
print(self.ticker)
INTC = Stock('INTC')
print(INTC.ticker)
MSFT = Stock('MSFT')
print(MSFT.ticker)
An instance of a class may call a function defined in it's own instance.
In [18]:
class Stock:
base_request_url = 'https://financialmodelingprep.com/api/v3/'
ticker = 'APPL'
def __init__(self,ticker):
self.ticker = ticker
self.daily_close = []
def return_ticker(self):
print(self.ticker)
def get_last_close(self):
return 10
def add_daily_close(self,last_close=None):
if last_close is None:
last_close = self.get_last_close()
self.daily_close.append(last_close)
INTC = Stock('INTC')
print(INTC.daily_close)
# when no value is passed it will call the get_last_close function which will return 10
INTC.add_daily_close()
print(INTC.daily_close)
# when a value is passed it will append that value to the daily close list
INTC.add_daily_close(55)
print(INTC.daily_close)
In python there is no such thing as a private variable, however there is a convention that developers commonly use to denote a private variable.
In [22]:
class Stock:
ticker = 'APPL'
def __init__(self,ticker):
self.ticker = ticker
self.daily_close = []
# a single underscore usually notes a variable that is private, and should not be changed
self._base_request_url = 'https://financialmodelingprep.com/api/v3/'
def return_ticker(self):
print(self.ticker)
def get_last_close(self):
return 10
def add_daily_close(self,last_close=None):
if last_close is None:
last_close = self.get_last_close()
self.daily_close.append(last_close)
INTC = Stock('INTC')
INTC._base_request_url
Out[22]:
If you would like to look at the instance attributes, you can use the dict with two leading and trailing underscores.
In [23]:
INTC.__dict__
Out[23]:
If you would like to look at all attributes of an instance, you can use class followed by dict.
In [26]:
INTC.__class__.__dict__
Out[26]: