클래스 vs 함수, 함수만 쓰면 되지 클래스를 도대체 왜 써야하는거야?
https://stackoverflow.com/questions/18202818/classes-vs-functions
Functions are easy to understand even for someone without any programming experience, but with a fair math background. On the other hand, classes seem to be more difficult to grasp.
Let's say I want to make a class/function that calculates the age of a person given his/her birthday year and the current year. Should I create a class for this, or a function? Or is the choice dependant to the scenario?
P.S. I am working on Python, but I guess the question is generic.
-
Classes are for bigger product. In simple terms, thinking of nut and bolt of a car as objects, while even car is an object too. If you are writing sample programs for fun, stick with functions. – Saran-san Aug 13 '13 at 7:17
-
1I wrote up my thoughts here – Toby May 1 '14 at 19:15
答えが見つからない?日本語で聞いてみましょう。
Create a function. Functions do specific things, classes are specific things.
Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.
Essentially, a class is a way of grouping functions (as methods) and data (as properties) into a logical unit revolving around a certain kind of thing. If you don't need that grouping, there's no need to make a class.
-
4This is not exactly true. Classes also allow for dispatching on types. If you have a group or template of collaborating functions it may well be a one shot execution but nevertheless using a class buys you the possibility of selectively implementing different functions in the group in order to get specializations of the template. This is not so easily done by plain functions, because there is no type to act as a link between them and to allow specialization on subtypes... – memeplex Mar 1 '16 at 7:08
-
... Forcing your logic, you can include the class functions (for example as a vtable) as part of the state or data of the instance. The functions themselves are not stored within the instance but some sort of indirect reference to them instead. So the bottom line is that you may have no data (besides a pointer to a vtable), you may have no more than an instance, but still a class could be a good idea if you need to implement a family of related algorithms consisting of collaborating functions. – memeplex Mar 1 '16 at 7:09
-
To summarize: a
class
groups functions and data. There is no benefit to this under any circumstance. We have functions and data types and modules to group them together. Even if you need adhoc function polymorphism: different function implementations for different data types, there are much better solutions than OOP classes; consider Haskell-style type classes which are ultimately language agnostic. – clay May 23 '16 at 15:46
위의 성님들이 발화를 정리하면..
클래스는 여러 함수를 묵는 논리적 단위이다. 함수를 그루핑해서 이득을 볼 수 있는 것이 아니라면 굳이 클래스를 사용할 필요가 없다.
그럼 클래스는 어떤 기능을 하는 걸까?
1. java와 같은 언어에서 Class는 type을 선언하는 기능이 있다.
2. 인스턴스를 생성해서 값을 초기화하고, 변수를 돌려 사용할 수 있다.
3. 상속을 사용하여, 효율적인 함수의 재활용이 가능하다.
4. 추상클래스가 존재하여 개발자에게 필수적인 함수의 틀을 제공하고 개발을 강제화할 수 있어 안정성이 높다.
오늘의 본문, 파이썬이나 자바스크립트에서는 클래스를 사용해야할 이유가 있을까?
먼저, 파이썬이나 자바스크립트는 class type이 존재하지 않기 때문에, 1번과 같은 장점은 얻을 수 없다.
1. java와 같은 언어에서 Class는 type을 선언하는 기능이 있다.
2. 인스턴스를 생성해서 값을 초기화하고, 변수를 돌려 사용할 수 있다.
3. 상속을 사용하여, 효율적인 함수의 재활용이 가능하다.
4. 추상클래스가 존재하여 개발자에게 필수적인 함수의 틀을 제공하고 개발을 강제화할 수 있어 안정성이 높다.
그래도 요건에 따라, 2,3,4 정도의 클래스를 사용하는 이득은 볼 수 있을 듯하다.
'C Lang > Python Program Diary' 카테고리의 다른 글
에러코드와 에러메세지를 갖는 에러생성하기 (0) | 2019.06.27 |
---|---|
파이썬에서 JSON의 decoding과 encoding은 어떻게 할까? json.load, json.dumps. (0) | 2019.06.26 |
Python’s Requests Library (Guide) (0) | 2019.06.19 |
python program diary (0) | 2019.06.16 |
파이썬 logging入門 (0) | 2019.06.14 |