โœŒ๏ธ
Studylog
See More
Studylog
Studylog
  • INTRO
  • AWS
    • AWS101
      • Virtualization and the AWS structures
      • AWS account and free-tier
      • AWS IAM
      • AWS EC2
        • EC2 basic
        • ENI and EIP
        • Elastic Block Storage
        • Auto Scaling
        • Elastic Load Balancer
  • IaC
    • Terraform
      • License Change
      • Tutorial
      • Module
      • Versioning
  • Airflow
    • Airflow-Ninja
      • Introduction and Goal
      • Tutorial: Settings
      • Tutorial: Module Import, Alert
  • Docker
    • Production with Docker
      • Customizing root directory
  • Network
    • Network-Handbook
      • Introduction and Goal
      • OSI7Layer
      • DNS
      • SSL/TLS
  • Comupter Architecture
    • Basic
      • Introduction and Goal
      • Chapter 1. The Internal Language of Computers
      • Chapter 2. Combinatorial Logic
      • Chapter 3. The Essentials of Memory and Disk Sequential Logic
    • Hands-on
      • Introduction and Goal
      • theory
        • Chapter 1. Logic Gates
        • Chapter 2. ALU
      • project
        • Project 1. Elementary logic gates implement
        • Project 2. Boolean Arithmetic
  • Java
    • Readable Code
      • ํ•™์Šต ๋ชฉ์ 
      • ์ถ”์ƒํ™”
      • ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ํ๋ฆ„
      • ๊ฐ์ฒด์ง€ํ–ฅ ์ด๋ก 
      • ๊ฐ์ฒด์ง€ํ–ฅ ์ฝ”๋“œ ์ ์šฉํ•˜๊ธฐ
      • ์ฝ”๋“œ ๋‹ค๋“ฌ๊ธฐ
      • ์ฝ๊ธฐ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ๋„์™€์ค„ ์กฐ์–ธ๋“ค
  • Spring Boot
    • Practical Testing
      • ํ…Œ์ŠคํŠธ ์‚ฌ์ „ ์ง€์‹
      • ์Šคํ”„๋ง ๋ ˆ์ด์–ด๋“œ ์•„ํ‚คํ…์ฒ˜ ํ…Œ์ŠคํŠธํ•˜๊ธฐ
        • Persistence Layer
        • Business Layer
    • ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ
      • ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ์Šคํ”„๋ง
      • ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ ์ดํ•ด
        • ์˜ˆ์ œ ๋งŒ๋“ค๊ธฐ
        • ๊ฐ์ฒด ์ง€ํ–ฅ ์›๋ฆฌ ์ ์šฉ
        • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์™€ ์Šคํ”„๋ง ๋นˆ
  • Python
    • Effective Python
      • Introduction and Goal
      • Closure: Lazy Evaluation And Eager Evaluation
      • Python public attributes are better getter-setter
      • No refactoring attribute, we can use property decorator
      • You can do it, but it means you don't have to
  • Playgrounds
    • Java Playground
      • ํ•™์Šต ๋ชฉ์ 
      • ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„
        • ํ•™์Šต ํ…Œ์ŠคํŠธ
        • ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ
        • ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„ ๊ตฌํ˜„
        • ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜๊ธฐ
      • ์ž๋™์ฐจ ๊ฒฝ์ฃผ
        • ๋ฌธ์ž์—ด ๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ
        • ์ž๋™์ฐจ ๊ฒฝ์ฃผ ๋ฏธ์…˜ ๊ตฌํ˜„
      • ์ขŒํ‘œ ๊ณ„์‚ฐ๊ธฐ
        • ์—ฐ๋ฃŒ ์ฃผ์ž…
        • ์ขŒํ‘œ ๊ณ„์‚ฐ๊ธฐ ๋ฏธ์…˜ ๊ตฌํ˜„
    • Infra Playground
      • VPC: ๋ง๋ถ„๋ฆฌ ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ
      • ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ „ ์ง€์‹
      • ํ™”๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„  ์ „ ํ•™์Šต ํ…Œ์ŠคํŠธ
      • SSM: Session Manager
      • SQL, ์ด ์ •๋„๋Š” ์•Œ์•„์•ผ์ง€ ๐Ÿ˜Ž
      • Subway-Map
        • ๋ง ๊ตฌ์„ฑํ•˜๊ธฐ
        • ์„œ๋ฒ„ ๊ตฌ์„ฑํ•˜๊ธฐ
        • ํ™”๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„ ํ•˜๊ธฐ
      • Conference Platform
        • ๋ง ๊ตฌ์„ฑํ•˜๊ธฐ
        • ์„œ๋ฒ„ ๊ตฌ์„ฑํ•˜๊ธฐ
        • ํ™”๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„ ํ•˜๊ธฐ
  • Tools
    • SOPS
    • Bruno
    • ๐Ÿ–ฅ๏ธFCK-NAT
    • ๐ŸงŠPulumi
Powered by GitBook
On this page
  • Introduction
  • Goal
  • Review
  • Summary
  1. Python
  2. Effective Python

Python public attributes are better getter-setter

PreviousClosure: Lazy Evaluation And Eager EvaluationNextNo refactoring attribute, we can use property decorator

Last updated 10 months ago

Properties

2024.03.24

BETTER WAY 45

Introduction

  • ์„ธํ„ฐ์™€ ๊ฒŒํ„ฐ ๋ฉ”์„œ๋“œ ๋Œ€์‹  ํ‰๋ฒ”ํ•œ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ

Goal

  • ํŒŒ์ด์ฌ์—์„œ ๊ฒŒํ„ฐ-์„ธํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ , ๊ณต๊ฐœ์  ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„ํŽธํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ž.

  • ํ”„๋กœํผํ‹ฐ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•œ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์„ ์ ์šฉํ•œ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ž

Worst case

ํ†ต์ƒ์ ์ธ ๊ฒŒํ„ฐ-์„ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์ด์ง€๋งŒ, ํŒŒ์ด์จ๋‹‰ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ์˜ ์˜ˆ์‹œ์ด๋‹ค.

class OldResistor:
	def __init__(self, ohms):
		self._ohms = ohms

	def get_ohms(self):
		return self._ohms

	def set_ohms(self, ohms):
		return self._ohms = ohms

if __name__ == "__main__":
	r0 = OldResistor(50e-3)
	print(f"์ด์ „: {r0.get_ohms()}")
	
	r0.set_ohms(10e3)
	print(f"{r0.get_hms()}")  
  • get - set๊ตฌ์กฐ์˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ๊ธฐ๋Šฅ์„ ์บก์Аํ™” ํ•˜๋Š” ๊ฒฝ๊ณ„๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

  • ํŒŒ์ด์ฌ์—์„œ๋Š” ์œ„ ์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์ „ํ˜€ ์—†๋‹ค.

Solution

๊ณต๊ฐœ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ด์šฉํ•˜๊ธฐ

class Resitor:
	def __init__(self, ohms):
		self.ohms = ohms
		self.voltage = 0
		self.current = 0

if __name__ == "__main__":
	r1 = Resistor(50e-3)
	r1.ohms = 10e3
	r1.ohms += 5e3
  • ์œ„ ์ฝ”๋“œ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ฒŒํ„ฐ ์„ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฑ„ ๊ณต๊ฐœ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ด์šฉํ–ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๊ต‰์žฅํžˆ ๊น”๋”ํ•ด์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

  • ๋งŒ์•ฝ, ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์„ค์ •ํ•  ๋•Œ ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ํ”„๋กœํผํ‹ฐ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Best case - 1

property decorator ์‚ฌ์šฉ ํ•˜์—ฌ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ํ•˜๊ธฐ

class VoltageResistance(Resistor):
	def __init__(self, ohms):
		super().__init__(ohms)
		self._voltage = 0

	@property
	def voltage(self):
		return self._voltage

	@voltage.setter
	def voltage(self, voltage):
		self._voltage = voltage
		self.current = self._voltage / self.ohms

if __name__ == "__main__":
	r2 = VoltageResistance(1e3)
	print(f"์ด์ „: {r2.current:.2f} ์•”ํŽ˜์–ด")

	r2.voltage = 10
	print(f"์ดํ›„: {r2.current:.2f} ์•”ํŽ˜์–ด")

Best case - 2

Property๋ฅผ ํ™œ์šฉ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ฒ€์ฆํ•˜๊ธฐ

class BoundedResistance(Resistor):
	def __init__(self, ohms):
		super().__init__(ohms)

	@property
	def ohms(self):
		return self._ohms

	@ohms.setter
	def ohms(self, ohms):
		if ohms <= 0:
			raise ValueError(f"์ €ํ•ญ > 0์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’: {ohms}")
		self._ohms = ohms

if __name__ == "__main__":
	r3 = BoundedResistance(-5)
	r3.ohms = 0

# >> ValueError!

Review

  • ๊ฒŒํ„ฐ๋‚˜ ์„ธํ„ฐ๋ฅผ ์ •์˜ ํ•  ๋•Œ ๊ฐ€์žฅ ์ข‹์€ ์ •์ฑ… ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฐ์ฒด ์ƒํƒœ๋ฅผ property setter๋ฉ”์„œ๋“œ ์•ˆ์—์„œ๋งŒ ๋ณ€๊ฒฝ ํ•œ๋‹ค.

  • ๋” ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋А๋ฆฐ ์—ฐ์ƒ์˜ ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

    • I/O, DB DML

Summary

  • ์ƒˆ๋กœ์šด ํด๋ž˜์Šค ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ ํ•  ๋•Œ๋Š” ๊ฐ„๋‹จํ•œ ๊ณต๊ฐœ ์• ํŠธ๋ฆฌ๋ทฐํŠธ์—์„œ ์‹œ์ž‘ํ•˜๊ณ , ์„ธํ„ฐ๋‚˜ ๊ฒŒํ„ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ.

  • ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๋Š” ์• ํŠธ๋ฅ˜๋น„ํŠธ์˜ ํŠน์ •ํ•œ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด property decorator๋ฅผ ํ™œ์šฉํ•˜๋ผ.

  • property decorator๋ฅผ ๊ตฌ์„ฑํ•  ๋• ๋ฉ”์„œ๋“œ๊ฐ€ ๋น ๋ฅด๊ฒŒ ์‹คํ–‰ ๋˜๋„๋ก ์œ ์ง€ํ•˜๋ผ.

๐Ÿ“
๐Ÿ“„
๐Ÿ“Ž
ํŒŒ์ด์ฌ ์ฝ”๋”ฉ์˜ ๊ธฐ์ˆ : ๋˜‘๋˜‘ํ•˜๊ฒŒ ์ฝ”๋”ฉ ํ•˜๋Š” ๋ฒ• 2ํŒ