Chapter 10 Object Oriented Programming
Class and instance
Attributes
Instance method and static method
10.1 Basic concept
想定義「黃色方形」物件類別:
這類物件有什麼共通屬性:黃色
各別物件有什麼基本屬性:height, width, center.
對它有什麼想運用的操作:算它的面積
class YellowRectangle:
""" A Python object that describes the properties of a yellow rectangle """
color="yellow"
def __init__(self, width, height):
self.width = width
self.height = height
def __repr__(self):
return "Rectangle(width={w}, height={h})".format(h=self.height, w=self.width)
def compute_area(self):
return self.width * self.height
10.2 Class and Instance
判斷class或instance:
YR=YellowRectangle # YR is a class object
yr1=YR(5,10) # yr1 is an instance
yr2=YellowRectangle(1,3)
## True
## False
## True
## True
10.3 Class attributes and instance attributes
透過
def __init__(self,...)
定義的attribute為instance attributes.其他
def __init__(self,...)
以外定義的object都會成為class attributes.class attribute自然會是instance attribute
以下何者為class attr?何者為instance attr?
在一個父姓社會,定義linFamily這個class,每個instance是一個人:
所有的instance一定姓“林”:即
self.surname="林"
每個instance有自己的名:即
self.firstname
因人而異每個instance有自己的性別:即
self.gender
因人而異每個instance print時會顯示“我叫XXX, 性別Y”
10.4 Instance and static method
- Instance method:
def ...(self,...)
: 第一個arg為self.self
代表了instance本身。
An instance method is defined whenever a function definition is specified within the body of a class. This may seem trivial but there is still a significant nuance that must be cleared up, which is that ‘self’ is the defacto first-argument for any instance method.
When you call an instance method (e.g. func) from an instance object (e.g. inst), Python automatically passes that instance object as the first argument, in addition to any other arguments that were passed in by the user.
因此每個class body裡的def最好都加上
self
成第一個input。
- static method:
def ...(....)
: 沒有用self(或cls,以後會講class method)在arg裡。
A static method is simply a method whose arguments must all be passed explicitly by the user. That is, Python doesn’t pass anything to a static method automatically.
A static method is simply a method whose arguments must all be passed explicitly by the user. That is, Python doesn’t pass anything to a static method automatically. The built-in decorator @staticmethod
is used to distinguish a method as being static rather than an instance method.
class Dummy:
@staticmethod
def static_func():
""" A static method defined to always returns
the string `'hi'`"""
return 'hi'
## 'hi'
在Basic concept範例裡,指出當中的instance method/static method。
class H5rectangle:
height=5 # 所有H5rectangle族群高都是5
def __init__(self,width): #旅群內的寬每個人都不同,有自己self的寬
self.width=width
def area(self): # 每個人都可以用area method來算自己的面積
return self.width*self.height
延續linFamily class。
- 每個林家人有marry method,即
instance.marry(spouse)
。此method會增加instance.spouse
屬性。
child1=linFamily("小明","男")
wife=wife={"surname": "陳", "familyname": "小美", "gender": "女"}
child1.marry(wife)
child1.spouse
## {'surname': '陳', 'familyname': '小美', 'gender': '女'}
10.5 class method
cls
as the first argumentdeclare
@classmethod