블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
  • total
  • today
  • yesterday

Category

2007. 7. 11. 14:34 Computation/Language
ref. Python tutorial 9. Classes

The class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain an arbitrary amount of private data.

In Python, all data types are objects.

A namespace is a mapping from names to objects. Name spaces are created at different moments and have different lifetimes.
The word attribute is for any name following a dot. Attributes may be read-only or writable.
A scope is a textual region of a Python program where a namespace is directly accessible.

Assignments always go into the innermost scope. Assignments do not copy data -- they just bind names to objects.

A class object is basically a wrapper around the contents of the namespace created by the class definition.

Class objects support two kinds of operations: attribute references and instantiation.
Attribute references
use the standard syntax used for all attribute references in Python: obj.name.
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class.

The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names, data attributes and methods.
Data attributes
correspond to "instance variables'' in Smalltalk, and to "data members'' in C++.
A method is a function that "belongs to'' an object. The special thing about methods is that the object is passed as the first argument of the function.

Any function object that is a class attribute defines a method for instances of that class.

The global scope associated with a method is the module containing the class definition. (The class itself is never used as a global scope!) Functions and modules imported into the global scope can be used by methods, as well as functions and classes defined in it. Usually, the class containing the method is itself defined in this global scope.



ref. 이강성 <열혈강의 파이썬(Python)> 개정판 Ver.2: 12 클래스, 프리렉

파이썬의 클래스는 새로운 이름 공간을 지원하는 단위이다. 이 이름 공간에는 함수와 변수가 포함될 수 있다. 클래스는 하나 이상의 인스턴스 객체를 생성하는 틀과 같다.
(클래스 자체가 이름 공간을 가지지만, 각각의 인스턴스도 독립적인 이름 공간을 가진다. ->) 동적으로 클래스 외부에서 멤버를 생성할 수 있다.

Class : class 문으로 정의되며, 멤버와 메쏘드를 가지는 객체
Class Object
Class Instance : 클래스를 호출하여 만들어지는 객체
Class Instance Object
Member : 클래스가 갖는 변수
Method : 클래스 내에 정의된 함수
Attribute : 멤버와 메쏘드 전체. 즉, 이름 공간의 이름 전체.
Super Class = Base Class
Sub Class = Derived Class

> Features of Classes in OOP
Inheritance 상속: (A 클래스를 수퍼 클래스로 하는 클래스 B를 생성하였다면 B 'is-a' A 관계라고 함.)
Multiple Inheritance 다중 상속: 두 개 이상의 수퍼 클래스로부터 상속받는 것
Polymorphism 다형성: 상속 관계 내의 다른 클래스의 인스턴스들이 같은 멤버 함수 호출에 대해 각각 다르게 반응하도록 하는 기능
Encapsulation 정보 은닉: 메쏘드와 멤버를 클래스 내에 포함시키고 외부에서 접근 가능하도록 인터페이스만을 공개하고 다른 속성들은 내부에 숨기는 것
Composition 합성: 어떤 객체가 다른 객체에 포함되어 활용되는 것 (x라는 객체가 클래스 A안에 포함되어 A의 각종 메쏘드를 구현하는데 사용된다면, A가 x를 포함하므로 'has-a'관계라고 함.)

self: 메쏘드를 정의할 때 사용하는 첫 인수. 자신의 인스턴스 객체를 가리키는 레퍼런스이다.
- 모든 메쏘드는 반드시 self를 첫 인수로 받아야 한다. (이 self를 이용하여 클래스의 이름 공간에 접근해야 하기 때문이다.)
- 지역(local) 이름은 함수가 종료되면 리턴과 동시에 없어지므로 객체 이름 공간에 값을 저장해 두지 않으면 나중에 사용할 수 없다.
- 메쏘드를 인스턴스 객체를 통하여 직접 호출할 때는 첫번째 인수인 self는 없다고 생각하면 된다.
- 클래스의 멤버나 메쏘드를 참조하려면 언제나 self를 이용해야 한다.

Unbound Class Method 호출: 클래스 객체를 이용하여 메쏘드를 호출하는 것
Bound Instance Method 호출: 인스턴스를 통하여 자동으로 self인수를 전달하는 방식

Static Method 정적 메쏘드: 인스턴스 객체를 생성하지 않고도, 또는 인스턴스 객체를 이용하지 않고도 클래스 이름을 이용하여 직접 호출할 수 있는 메쏘드
Class Method

Class Member - 메쏘드 바깥에 정의된다.
Instance Member - 메쏘드 내부에서 self를 이용하여 정의된다.

Constructor 생성자: 인스턴스 객체가 생성될 때 초기화를 위해서 자동적으로 불려지는 초기화 함수
Destructor 소멸자: 인스턴스 객체가 사용이 끝나서 메모리에서 해체될 때 자원 해제를 위해서 자동으로 호출되는 함수

Reserved Words 예약어: 미리 어떤 기능이 정의된 경우
    eg1. __init__
    eg2. __del__







'Computation > Language' 카테고리의 다른 글

Python tutorials  (0) 2008.02.24
Using Python for CGI programming  (0) 2007.07.11
Python  (0) 2007.06.26
features of object-oriented programming  (0) 2007.06.20
내 컴퓨터에서 Processing의 source code 보기  (0) 2007.04.19
posted by maetel