Regarding class and inheritance, in Python for data analysis, we don't use them a lot, however, if you want to handle Django, it is necessary to know about classes and inheritance.

 

Class Example1

class Animal:
    name = 'dog'              
    age = 5

a1 = Animal()
# print(name)               
print(a1.name)                
print(a1.age)                 

a1.name = 'cat'
a1.age = 10
print(a1.name)               
print(a1.age)                

a2 = Animal()
print(a2.name)                
print(a2.age)

Class Example2

self. is similar to this. in Java.

class Car:

    def __init__(self):             # Constructor
       self.color = 'red'           
       self.wheel_size = 16        
       self.displacement = 2000     

    def forward(self):              # Method
        print('D')
    def backward(self):
        print('R')
    def turn_left(self):
        print('L')
    def turn_right(self):
        print('RR')

car1 = Car()                       

print(car1.color)                   # red
print(car1.wheel_size)              # 16
print(car1.displacement)            # 2000

car1.forward()                     
car1.backward()                     
car1.turn_left()                    
car1.turn_right()

Instance 

if __name__ == "__main__" allows You to Execute Code When the File Runs as a Script but Not When It’s Imported as a Module. You cannot import this in the other files if the file contains if __name__ == "__main__".

# if __name__=='__main__':

class InstanceVar:
    def __init__(self):             # Constructor
        self.text_list = []         

    def add(self, *text):          
        self.text_list.append(text)

    def print_list(self):           
        print(self.text_list)

if __name__=='__main__':
    a = InstanceVar()               
    a.add('Python', 'Oracle', 'Spring')
    a.print_list()                  
    print(a.text_list)              

    b = InstanceVar()              
    b.add('Java','JSP','tensorflow','keras')
    b.print_list()                 
    print(b.text_list)

Without if __name__ == "__main__"

def add(a, b):
    return a+b

def sub(a, b):
    return a-b

print(add(20, 10))              # 30
print(sub(20, 10))              # 10

With if __name__ == "__main__"

With this if statement, you cannot import in the other files. 

def add(a, b):
    return a+b

def sub(a, b):
    return a-b

if __name__=='__main__':
    print(add(20, 10))              # 30
    print(sub(20, 10))              # 10

if __name__ == "__main__" Example1

class ContactInfo:
    def __init__(self, name, email):           	# Constructor with parameters
        self.name = name                        
        self.email = email                      

    def print_info(self):
        print('{0} : {1}'.format(self.name, self.email))

if __name__=='__main__':
    ahn = ContactInfo('Zac','hong@naver.com')  
    ahn.print_info()
    print('name:', ahn.name)
    print('email:', ahn.email)

    ca = ContactInfo('choongang','master@gmail.com')
    ca.print_info()
    print('name:', ca.name)
    print('email:', ca.email)

Static Method

In java, @ is called an annotation whereas, in Python, we call it "Decorator".

When you use this decorator, it is a static method(@staticmethod). With this, you do not use self.

class Calculator:

    @staticmethod
    def plus(a,b):                 
        return a+b

    @staticmethod
    def minus(a,b):
        return a-b

    @staticmethod
    def multiply(a,b):
        return a*b

    @staticmethod
    def divide(a,b):
        return a/b

if __name__=='__main__':             
    print('{0}+{1}={2}'.format(7,4, Calculator.plus(7,4)))
    print('{0}-{1}={2}'.format(7,4, Calculator.minus(7,4)))
    print('{0}*{1}={2}'.format(7,4, Calculator.multiply(7,4)))
    print('{0}/{1}={2}'.format(7,4, Calculator.divide(7,4)))

 

'Python' 카테고리의 다른 글

Python) Database connection with MySQL  (0) 2022.11.20
Python) Database connection with SQLite  (0) 2022.11.19
Python) Database connection with Oracle  (0) 2022.11.18
Python) try: except:  (0) 2022.11.14
Python) list  (0) 2022.11.08

+ Recent posts