Classes

In [5]:
class MyGreatClass:
    """A simple example class"""
    i = 12345  # class (-level) variables
               # data shared between ALL instances of this class
    def func(self, num):
        return "hello world"
In [3]:
help(MyGreatClass)
Help on class MyGreatClass in module __main__:

class MyGreatClass(builtins.object)
 |  A simple example class
 |  
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

In [6]:
myc = MyGreatClass()
In [7]:
type(myc)
Out[7]:
__main__.MyGreatClass
In [8]:
dir(myc)
Out[8]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'func',
 'i']
In [9]:
dir(MyGreatClass)
Out[9]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'func',
 'i']
In [10]:
myc.i
Out[10]:
12345

Initializers (__init__)

In [22]:
class SimpleClass:
    """A simple class example"""  # docstring
    mult = 2  # class variables, shared by all instances of this class
    
    def __init__(self, nums):  # Initializer, method
        #print(id(self))
        
        #self.data = nums  # the easier way.
        self.data = []  # instance variable
        
        for n in nums:
            self.data.append(n)
            
        # __init__ *never* returns a value. Always 'None'
            
    def apply(self):
        for i in range(len(self.data)):
            self.data[i] = self.data[i] * self.mult
            
    def get_data(self):
        return self.data
In [23]:
x = SimpleClass([0,1,2,3,4])
In [24]:
id(x)
Out[24]:
4418588008
In [25]:
x.apply()
In [26]:
x.get_data()
Out[26]:
[0, 2, 4, 6, 8]
In [27]:
from random import sample
inst = []
for i in range(100):
    nums = sample(range(50), k=5)
    temp = SimpleClass(nums)
    inst.append(temp)
In [30]:
y = inst[0]
In [32]:
id(inst[0])
Out[32]:
4400103608
In [33]:
id(y)
Out[33]:
4400103608
In [34]:
inst[59]
Out[34]:
<__main__.SimpleClass at 0x107558908>
In [35]:
inst
Out[35]:
[<__main__.SimpleClass at 0x1064440b8>,
 <__main__.SimpleClass at 0x106444b70>,
 <__main__.SimpleClass at 0x105eeb320>,
 <__main__.SimpleClass at 0x107538358>,
 <__main__.SimpleClass at 0x107538438>,
 <__main__.SimpleClass at 0x107538eb8>,
 <__main__.SimpleClass at 0x107538588>,
 <__main__.SimpleClass at 0x107538ba8>,
 <__main__.SimpleClass at 0x107538518>,
 <__main__.SimpleClass at 0x1075386a0>,
 <__main__.SimpleClass at 0x107538208>,
 <__main__.SimpleClass at 0x1075383c8>,
 <__main__.SimpleClass at 0x107538a20>,
 <__main__.SimpleClass at 0x107538940>,
 <__main__.SimpleClass at 0x107538320>,
 <__main__.SimpleClass at 0x1075382e8>,
 <__main__.SimpleClass at 0x107538c18>,
 <__main__.SimpleClass at 0x107538278>,
 <__main__.SimpleClass at 0x107538978>,
 <__main__.SimpleClass at 0x107538550>,
 <__main__.SimpleClass at 0x1075382b0>,
 <__main__.SimpleClass at 0x107538470>,
 <__main__.SimpleClass at 0x107538198>,
 <__main__.SimpleClass at 0x107538860>,
 <__main__.SimpleClass at 0x1075386d8>,
 <__main__.SimpleClass at 0x107538710>,
 <__main__.SimpleClass at 0x1075387b8>,
 <__main__.SimpleClass at 0x107538668>,
 <__main__.SimpleClass at 0x107538828>,
 <__main__.SimpleClass at 0x107538fd0>,
 <__main__.SimpleClass at 0x107538898>,
 <__main__.SimpleClass at 0x107538b00>,
 <__main__.SimpleClass at 0x1075388d0>,
 <__main__.SimpleClass at 0x1075381d0>,
 <__main__.SimpleClass at 0x107538be0>,
 <__main__.SimpleClass at 0x107538da0>,
 <__main__.SimpleClass at 0x107538c88>,
 <__main__.SimpleClass at 0x1075385c0>,
 <__main__.SimpleClass at 0x107538160>,
 <__main__.SimpleClass at 0x107538f98>,
 <__main__.SimpleClass at 0x107538c50>,
 <__main__.SimpleClass at 0x107558828>,
 <__main__.SimpleClass at 0x107558da0>,
 <__main__.SimpleClass at 0x1075589e8>,
 <__main__.SimpleClass at 0x107558780>,
 <__main__.SimpleClass at 0x1075582e8>,
 <__main__.SimpleClass at 0x107558320>,
 <__main__.SimpleClass at 0x107558128>,
 <__main__.SimpleClass at 0x1075580f0>,
 <__main__.SimpleClass at 0x107558160>,
 <__main__.SimpleClass at 0x1075583c8>,
 <__main__.SimpleClass at 0x107558470>,
 <__main__.SimpleClass at 0x107558898>,
 <__main__.SimpleClass at 0x1075581d0>,
 <__main__.SimpleClass at 0x1075587b8>,
 <__main__.SimpleClass at 0x107558358>,
 <__main__.SimpleClass at 0x107558588>,
 <__main__.SimpleClass at 0x1075582b0>,
 <__main__.SimpleClass at 0x107558208>,
 <__main__.SimpleClass at 0x107558908>,
 <__main__.SimpleClass at 0x107558390>,
 <__main__.SimpleClass at 0x107558278>,
 <__main__.SimpleClass at 0x107558860>,
 <__main__.SimpleClass at 0x107558400>,
 <__main__.SimpleClass at 0x107558080>,
 <__main__.SimpleClass at 0x1075588d0>,
 <__main__.SimpleClass at 0x107558a90>,
 <__main__.SimpleClass at 0x107558b00>,
 <__main__.SimpleClass at 0x107558a20>,
 <__main__.SimpleClass at 0x107558940>,
 <__main__.SimpleClass at 0x1075589b0>,
 <__main__.SimpleClass at 0x107558978>,
 <__main__.SimpleClass at 0x107558a58>,
 <__main__.SimpleClass at 0x107566d68>,
 <__main__.SimpleClass at 0x1075667b8>,
 <__main__.SimpleClass at 0x1075667f0>,
 <__main__.SimpleClass at 0x107566828>,
 <__main__.SimpleClass at 0x107566940>,
 <__main__.SimpleClass at 0x107566978>,
 <__main__.SimpleClass at 0x1075669b0>,
 <__main__.SimpleClass at 0x1075669e8>,
 <__main__.SimpleClass at 0x107566a20>,
 <__main__.SimpleClass at 0x107566a58>,
 <__main__.SimpleClass at 0x107566a90>,
 <__main__.SimpleClass at 0x107566ac8>,
 <__main__.SimpleClass at 0x107566b00>,
 <__main__.SimpleClass at 0x107566b38>,
 <__main__.SimpleClass at 0x107566b70>,
 <__main__.SimpleClass at 0x107566ba8>,
 <__main__.SimpleClass at 0x107566be0>,
 <__main__.SimpleClass at 0x107566c18>,
 <__main__.SimpleClass at 0x107566c50>,
 <__main__.SimpleClass at 0x107566c88>,
 <__main__.SimpleClass at 0x107566898>,
 <__main__.SimpleClass at 0x107566860>,
 <__main__.SimpleClass at 0x107566908>,
 <__main__.SimpleClass at 0x107566cc0>,
 <__main__.SimpleClass at 0x107566cf8>,
 <__main__.SimpleClass at 0x107566d30>,
 <__main__.SimpleClass at 0x1075d6470>]