0915 TIL

looggiΒ·2022λ…„ 9μ›” 15일
1
post-thumbnail

β­β“β­•βŒπŸ”₯πŸ“šπŸ˜΅βœ…β—

파이썬 κ°•μ˜ by μ΄μ°½ν˜ΈνŠœν„° day6

  • try-except 문은 λ”± ν•„μš”ν•œ 곳에 λ“€μ–΄κ°€μ•Όν•œλ‹€!
try:
    cal=calculator(20,r)
    print(cal.div())
except ZeroDivisionError:
    print('0으둜 λ‚˜λˆŒ 수 μ—†μŠ΅λ‹ˆλ‹€')
except:
    print('숫자만 μž…λ ₯ κ°€λŠ₯ν•©λ‹ˆλ‹€')

-> μ΄λ ‡κ²Œ μΈμŠ€ν„΄μŠ€ μ΄ν•˜λ₯Ό λ‹€ κ°μ‹ΈλŠ” 것 보단
클래슀 μ•ˆμ— μ •μ˜λœ ν•¨μˆ˜ κ°κ°μ—μ„œ λ°œμƒν•˜λŠ” μ—λŸ¬λ‹ˆκΉŒ 각각의 ν•¨μˆ˜λ₯Ό μ•ˆμͺ½μ— μ“°λŠ” 것이 더 μ’‹λ‹€
->그리고 ν•„μš”ν•˜λ‹€λ©΄ try ν•˜λ‚˜μ— except μ—¬λŸ¬κ°œ 쀄쀄이 달아 μ“Έ μˆ˜λ„ μžˆλ‹€

  • 값을 μ£ΌλŠ”κ±΄ 인자(arguments) 받을 땐 λ§€κ°œλ³€μˆ˜(parameter)
def print_user_input(message): #parameter
	print(message)
print_user_input('hello sparta') #arg
  • Noneκ³Ό ''λŠ” λ‹€λ₯΄λ‹€!
    ❓null = True -> none을 ν—ˆμš©ν•¨
    blank = True -> 빈문자 ''λ₯Ό ν—ˆμš©ν•¨
  • option! κΈ°λ³Έ 섀정이 μžˆλ‹€λŠ” 것은 μ‚¬μš©μžκ°€ 원할 λ•Œ ν•„μš”ν•œ λΆ€λΆ„λ§Œ λ°”κΏ”μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•΄μ€Œ
  • args: μž…λ ₯값이 λΆˆκ·œμΉ™ν•œ 경우 μ‚¬μš©
  • kwargs: key word args λ”•μ…”λ„ˆλ¦¬ ν˜•νƒœλ‘œ 받을 수 μžˆλŠ” λ³€μˆ˜λ“€μ„ 받을 수 μžˆλ‹€
def main(n1,n2,*args,**kwargs)
	print(n1) #1
    print(n2) #2
    print(args) #(3,4,5,6,7,8,9)->νŠœν”Œλ‘œ!!
    print(kwargs) #{}->빈 λ”•μ…”λ„ˆλ¦¬λ‘œ!!
    
main(1,2,3,4,5,6,7,8,9)
  • unpacking: λ¦¬μŠ€νŠΈλ‚˜ λ”•μ…”λ„ˆλ¦¬ 값을 ν•¨μˆ˜μ— μž…λ ₯ν•  λ•Œ μ‚¬μš©
a = [1,2,3,4]
print(*a) #1,2,3,4

--> μœ„μ—μ„œ μ‚¬μš©ν•œ *args, **kwargs도 μ–ΈνŒ¨ν‚Ή

  • 객체지ν–₯ Object-Oriented-Programming
    - 클래슀 μƒμ†μœΌλ‘œ μ½”λ“œμ˜ μž¬μ‚¬μš©(같은 μ½”λ“œλ₯Ό μˆ˜μ •, ν•„μš”ν•œ μ½”λ“œ μΆ”κ°€) κ°€λŠ₯
    - λ°μ½”λ ˆμ΄ν„°λ₯Ό μ™œ ν•˜λ”λΌ..?❓
#regular expression: μ •κ·œμ‹
import re

# μ•ŒνŒŒλ²³, 숫자둜 μ‹œμž‘ν•΄μ•Ό ν•˜λ©° . 뒀에 2자의 μ•ŒνŒŒλ²³μ΄ 와야 ν•œλ‹€.
email_regex = re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')

class Attendance:
    count = 0
        
    def attendance(self):
        self.count += 1
    #λ°μ½”λ ˆμ΄ν„°!! ν”„λ‘œνΌν‹°μ™€ μ„Έν„°
    @property
    def attendance_count(self):
        return self.count

class Profile(Attendance):
    def __init__(self, name, age, email):
        self.__name = name # __λ₯Ό λΆ™μ—¬μ£Όλ©΄ class λ‚΄λΆ€μ—μ„œλ§Œ μ‚¬μš©ν•˜κ² λ‹€λŠ” 뜻
        self.__age = age #_λ₯Ό λΆ™μ—¬μ£Όλ©΄ class λ‚΄λΆ€μ—μ„œλ§Œ μ‚¬μš©ν•˜μžλŠ” 약속(κ°•μ œx ν•˜μ§€λ§Œ λ°–μ—μ„œ μ‚¬μš©μ‹œ μΆ”ν›„ 였λ₯˜λ°œμƒκ°€λŠ₯)
        self.__email = email
    
    @property # 읽기 μ „μš© 속성- ν˜ΈμΆœμ‹œ μ•„λž˜κ°€ 싀행됨
    def name(self):
        return self.__name
    
    @name.setter # μ“°κΈ° μ „μš© 속성- 값을 μ„€μ •ν•  λ•Œ μ•„λž˜κ°€ 싀행됨
    def name(self, name):
        if isinstance(name, str) and len(name) >= 2:
            print("이름은 2자 이상 문자만 μž…λ ₯ κ°€λŠ₯ν•©λ‹ˆλ‹€.")
            
        else:
            self.__name = name
    
    @property
    def age(self):
        return self.__age
    
    @age.setter
    def age(self, age):
        if isinstance(age, int):
            self.__age = age
            
        else:
            print("λ‚˜μ΄μ—λŠ” 숫자만 μž…λ ₯ κ°€λŠ₯ν•©λ‹ˆλ‹€.")
        
    @property
    def email(self):
        return self.__email
    
    @email.setter
    def email(self, email):
        if re.fullmatch(email_regex, email):
            self.__email = email
        else:
            print("μœ νš¨ν•˜μ§€ μ•Šμ€ μ΄λ©”μΌμž…λ‹ˆλ‹€.")
        
    def attendance(self): # override
        super().attendance() # λΆ€λͺ¨ λ©”μ†Œλ“œ μ‚¬μš©ν•˜κΈ°
        self.count += 1
    
    def __str__(self):
        return f"{self.__name} / {self.__age}"
    
    def __repr__(self):
        return f"{self.__name}"

  • return에 λŒ€ν•˜μ—¬
return print()

-> 같이 쓰지 μ•ŠλŠ”λ‹€ λ‘˜ 쀑 ν•˜λ‚˜λ§Œ! λ¦¬ν„΄ν•΄μ€˜μ„œ ν”„λ¦°νŠΈλŠ” λ‹€μŒ 차둀에 ν•˜κ±°λ‚˜ ν”„λ¦°νŠΈλ₯Ό ν•΄μ„œ ν•¨μˆ˜ μ‹€ν–‰ν•˜λ©΄ μžλ™μœΌλ‘œ ν”„λ¦°νŠΈ λ˜λ„λ‘ ν•œλ‹Ή
1)

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

2)

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

-> 1),2) 두가지 경우 λͺ¨λ‘ none을 λ¦¬ν„΄ν•œλ‹€

  • μΈμžκ°€ ν•„μš”ν•œ 곳을 λΉ„μ›Œλ‘λ©΄!
def add(a,b):
	print(a+b)

add()

-> missing 2 arguments

  • setν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄μ£ΌλŠ” 이유
    μ§„μ§œ λ§κ·ΈλŒ€λ‘œ μ„ΈνŒ…μ΄ ν•„μš”ν•œ 경우!
    - getν•¨μˆ˜λ₯Ό μ¨μ„œ λ°Έλ₯˜κ°’을 가져와야 기본값을 μ§€μ •ν•΄μ€˜μ•Ό(<κ°€ λ§žλŠ”λ“―..?) μ—λŸ¬κ°€ λ‚˜μ§€ μ•ŠλŠ”λ‹€β“
    - κ·Έλƒ₯ kwargs['name']으둜 ν•˜λ©΄ name값이 λΉ„μ–΄μžˆμ„ 경우 기본값이 μ—†κΈ°λ•Œλ¬Έμ— μ—λŸ¬κ°€λ‚œλ‹€
    -λ”•μ…”λ„ˆλ¦¬.get(key,value)
def set_profile(**kwargs):
    profile = {}
    profile["name"] = kwargs.get("name", "-")
    profile["gender"] = kwargs.get("gender", "-")
    profile["birthday"] = kwargs.get("birthday", "-")
    profile["age"] = kwargs.get("age", "-")
    profile["phone"] = kwargs.get("phone", "-")
    profile["email"] = kwargs.get("email", "-")
    
    return profile

user_profile = {
    "name": "lee",
    "gender": "man",
    "age": 32,
    "birthday": "01/01",
    "email": "python@sparta.com",
}

print(set_profile(**user_profile))

""" μ•„λž˜ μ½”λ“œμ™€ 동일, ν’€μ–΄μ„œ μ“΄ 것 λΏμž„
profile = set_profile(
          name="lee",
          gender="man",
          age=32,
          birthday="01/01",
          email="python@sparta.com",
)
"""
profile
looooggi

0개의 λŒ“κΈ€