<jupyter_start><jupyter_text>Object Oriented Programming in Python<jupyter_code>class PartyAnimal: # class name 
    # data
    x = 0

    # code
    def party(self):  
        self.x = self.x + 1 # class data 
        print("So far", self.x)
l1  = list()  # define a list object 
d1  = dict()  # define a dictionary instance 
a1 = PartyAnimal() # define a PartyAnimal object / instance 
l1.append(3)
a1.party() # make party 
 a1.party() # make party again :) 
a1.party() # one more time :)
a2 = PartyAnimal() # define another instance 
a2.party()<jupyter_output>So far 1<jupyter_text>Local varaibles in functions<jupyter_code>def f1():
    x = 10 
    print('x =', x)
f1()
print('x =', x) # name error. The scope of x is within the function<jupyter_output><empty_output><jupyter_text>The meaning of def party(self):<jupyter_code>a1.party()
PartyAnimal.party(a1) # the same 
print(type(l1))
print(type(a1))
print(dir(a1))<jupyter_output>['__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__', 'party', 'x']<jupyter_text>Access class field<jupyter_code>a1.x
a1.x = 10
a1.x<jupyter_output><empty_output><jupyter_text>Constructor / Destructor<jupyter_code>class PartyAnimal:
   x = 0

   def __init__(self): # constractor # init = initilize 
     print('I am constructed')

   def party(self) :
     self.x = self.x + 1
     print('So far',self.x)

   def __del__(self): # destractor 
     print('I am destructed', self.x)
a1 = PartyAnimal() # define object 
a1.party()<jupyter_output>So far 1<jupyter_text>To see the destructor, we define an object within a scope of a function<jupyter_code>def f1():
    a = PartyAnimal()
f1()
print('done')<jupyter_output>I am constructed
I am destructed 0
done<jupyter_text>Initialize object with constructor<jupyter_code>class PartyAnimal:
   x = 0
   name = ""
   def __init__(self, z):
     self.name = z
     print(self.name,"constructed")

   def party(self) :
     self.x = self.x + 1
     print(self.name,"party count",self.x)       
# define list object and with initial values
l1 = list([1,2,3])
print(l1)
# similarly
d = PartyAnimal("Dinsor")
h = PartyAnimal("Horse")
d.party()
h.party()
d.party()<jupyter_output>Dinsor party count 1
Horse party count 1
Dinsor party count 2
{'fraction_non_alphanumeric': 0.14160351017151973, 'fraction_numerical': 0.017949740725967292, 'mean_word_length': 4.548672566371682, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '294249', 'license': 'Apache-2.0', 'n_tokens_mistral': 977, 'n_tokens_neox': 934, 'n_words': 231, 'path': 'chapter14_OOP/2019/ObjectOrientedProgramming.ipynb', 'repo_name': 'motazsaad/WDMM1405', 'size': '2507'}