<jupyter_start><jupyter_text>Simple class<jupyter_code># simple class
class Person:
    pass # empty
p = Person()
print(p)
class Person:
    def say_hi(self):
        print('hello, how r u?')
Person().say_hi()<jupyter_output>hello, how r u?<jupyter_text>__init__使用<jupyter_code>class Person:
    def __init__(self, n):
        self.name = n

    def say_hi(self):
        print('hello, my name is:', self.name)
Person('AAA').say_hi()<jupyter_output>hello, my name is: AAA<jupyter_text>ObjectVariable<jupyter_code>class Robot:
    """表示有個帶名字的機器人"""
    #Class Variable, counting num of robot
    population = 0 
    
    def __init__(self, n):
        """初始化數據"""
        self.name = n 
        print('(initializing {})'.format(self.name))

        #當有人被創建時,機器人
        #增加人口數量
        Robot.population += 1
    def die(self):
        """I'm die"""
        print('{} is being destroyed!'.format(self.name))

        Robot.population -= 1

        if Robot.population == 0:
            print('{} was the last one.'.format(self.name))
        else:
            print('There are still {:d} robot working.'.format(Robot.population))
    
    def say_hi(self):
        """來自機器人的問候
        
        沒問題,你做得到."""
        print('Greetings, my master call me {}.'.format(self.name))

    @classmethod
    def how_many(cls):
        """當前數量"""
        print('We are {:d} robots.'.format(cls.population))

### create Robot 1
droid1 = Robot("R2-D2") #'Robot' is Class
droid1.say_hi() 
Robot.how_many()     

### create Robot 2

droid2 = Robot("C-3PO") #'Robot' is Class
droid2.say_hi()
droid2.how_many()


print("\nRobots can do some work here.\n")

droid2.die()
droid1.die()

Robot.how_many()<jupyter_output>(initializing R2-D2)
Greetings, my master call me R2-D2.
We are 1 robots.
(initializing C-3PO)
Greetings, my master call me C-3PO.
We are 2 robots.

Robots can do some work here.

C-3PO is being destroyed!
There are still 1 robot working.
R2-D2 is being destroyed!
R2-D2 was the last one.
We are 0 robots.<jupyter_text>Inheritance 繼承<jupyter_code>class SchoolMember: #base class / Superclass
    '''All school members'''
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print('(Initialized SchoolMember: {})'.format(self.name))

    def tell(self):
        '''Tell all details'''
        print('Name:"{}", age:"{}" '.format(self.name, self.age), end='') #end='' 不斷行

class Teacher(SchoolMember): #Dervied Classes / Subclass
    '''Each Teacher'''
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print('(Initialize Teacher: {})'.format(self.name))

    def tell(self):
        SchoolMember.tell(self)
        print('Salary: "{:d}"'.format(self.salary))

class Student(SchoolMember): #Dervied Classes / Subclass
    '''Each Student'''
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print('(Initialized Student: {})'.format(self.name))
    
    def tell(self):
        SchoolMember.tell(self)
        print('Marks: "{:d}"'.format(self.marks))

t = Teacher('<NAME>', 40, 30000)
s = Student('Swaroop', 25, 75)

members = [t, s]

for m in members:
    m.tell()<jupyter_output>(Initialized SchoolMember: <NAME>)
(Initialize Teacher: <NAME>)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)
Name:"<NAME>", age:"40" Salary: "30000"
Name:"Swaroop", age:"25" Marks: "75"
{'fraction_non_alphanumeric': 0.15413425525785077, 'fraction_numerical': 0.015269374819936617, 'mean_word_length': 3.1333333333333333, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '385986', 'license': 'MIT', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1243, 'n_words': 303, 'path': 'oop_init.ipynb', 'repo_name': 'jesperwang2369/A-Byte-of-Python', 'size': '3503'}