1. 程式人生 > >【Python】Learn Python the hard way, ex42 對、類及從屬關係

【Python】Learn Python the hard way, ex42 對、類及從屬關係

# Animal is-a object (yes, sort of confusing) look at the extra credit
class Animal(object):
    pass

# is-a
class Dog(Animal):
    def __init__(self, name):
        ## has-a
        self.name = name

# is-a
class Cat(Animal):
    
    def __init__(sefl, name):
        # has-a
        self.name = name
        
# is-a
class Person(object):
    
    def __init__(self, name):
        # has-a
        self.name = name
        
        ## Person has-a a pet of some kind
        self.pet = None
        
# is-a
class Emplyee(Person):
    
    def __init__(sefl, name, salary):
        ## ? hmm what is this strange magic?
        super(Emplyee, slef).__init__(name)
        ## has-a
        self.salary = salary
        
# is-a
class Fish(object):
    pass

# is-a
class Salmon(Fish):
    pass
    
# is-a
class Halibut(Fish):
    pass
    
# rover is-a Dog
rover = Dog("Rover")

# has-a
satan = Cat("Satan")

# has-a
mary = Person("Mary")

# has-a
mary.pet = satan

# has-a
frank = Employee("Frank", 120000)

# has-a
frank.pet = rover

# is-a
flipper = Fish()

# is-a
crouse = Salmon()

# is-a
harry = Halibut()

相關推薦

PythonLearn Python the hard way, ex42 從屬關係

# Animal is-a object (yes, sort of confusing) look at the extra credit class Animal(object): pass # is-a class Dog(Animal): def

《笨方法學 Python 3》42.物件從屬關係

有個重要的概念必須弄明白,那就是‘類’和‘物件’的區別。 原文中花費了大量語段來描述,我就按個人理解簡單的歸納一下,可能不對,如有誤,請指出: 1.類:就是具有相同屬性的方法的集合,此處的方法就是指的函式,一個類裡面包含大量的函式(方法),這些函式之間具有共同之處。 2

PythonLearn Python the hard way, ex47 自動化測試,環境變數設定

class Room(object): def __init__(self, name, description): self.name = name self.description = description

PythonLearn Python the hard way, ex50 第一個網站

利用lpthw.web框架實現"Hello World"網頁版,程式碼如下: import web urls = ('/', 'index') app = web.application(url

PythonLearn Python the hard way, ex40 模組,和物件,這個解釋比較容易理解

#coding:utf-8 ''' 理解類和模組 Python是一種面向物件程式設計(OOP)語言。這個說法的意思是,Python裡邊有一種叫做類(class)的結構,通過它可以用一種特殊的方法構造軟體。 通過使用類,可以讓程式架構更為整齊,使用起來也更加乾淨 -- 至少

Learn python3 the hard way》ex17

今天新學了一個os.path 這個包裡exists這個功能,驗證是否存在,存在則顯示TRUE不存在則顯示False。 from sys import argv from os.path import exists script, from_file, to_file = argv p

Learn python3 the hard way》ex16 讀寫檔案

作者想讓我們記住的命令: close- 關閉檔案,就像編輯器中的“檔案->另存為”一樣 read- 讀取檔案內容。你可以把讀取結果賦值給一個變數 readline- 只讀取文字檔案的一行內容 truncate- 清空檔案。清空的時候要當心 write("stuff")-給檔案寫入一些東

Learn python3 the hard way》ex15閱讀檔案

今天學習的是argv的filename屬性以及檔案的open和read,txt是在相同路徑內已存在的一個txt from sys import argv scrip, filename = argv txt = open(filename) #開啟路徑並賦值給txt print(

Learn python3 the hard way》ex14 Prompting and Passing

今天是使用提示語>以及運用argv 和 input進行互動。 from sys import argv script, user_name = argv prompt = ">" print(f"Hi {user_name},I'm the {script} script

Learn python3 the hard way》ex13 Parameters,Unpacking,Variables

from sys import argv # read the WYSS section for how to run this: script, first, second, third = argv print("The script is called:", script) print

Learn python3 the hard way》ex12 Prompting People

Prompting People 今天在敲這個程式碼的時候,我還在想我昨天是不是做了今天的份了,怎麼那麼像的,回頭一看還是有 區別的, 昨天是先提前將提示語打印出,末位需要用end=""連線才能不換行,再用input給變數賦值。 今天是直接在inp

Learn python3 the hard way》ex11 Asking Quesions

之前都是直接輸入列印結果的,這節課學習的是如何在程式碼執行時,由使用物件自行輸入,打印出結果,這就是一個簡單的互動。 方法input(),生成的是字串,如果需要使結果是數字用int(input()) print("How old are you?", end="") age = inpu

Learn python3 the hard way》ex10 What was that

\在python裡面表示轉義符,功能: 把python中的一些特殊字元有功能的字元打印出來,比如\可以打印出\ 可以擁有一些功能,比如換行\n,比如縮排\t tabby_cat = "\tI'm tabbed in." persian_cat

Learn python3 the hard way》ex9

Printing,Printing,Printing 今天學習的主要是如何打換行的文字 法一:換行符\n 法二 """""" 一對三引號,三個引號中間無空格 # Here's some new strange stuff, remember type it exactly. day

Learn Python3 the hard way 第一天總結 命令列(1)

附錄-命令列快速入門(1) command line interface 簡稱 CLI ,可以在mac OS 上通過一些輸入進行一些操作。 1如何在迷路後怎樣回家 命令: pwd:列印工作目錄cd 更改目錄 上機操作: 在 CLI 中輸入:pwdcd ~其中,pwd 列印當前的目錄,cd

06《learn Python3 the hard way》一起來敲程式碼,每天進步一點點

lesson 6 字串和文字 從今天開始打算每天學習《learn python3 the hard way》,並在部落格裡記錄筆記,希望能堅持下來,總共有52課,我記得,從第六課開始,以下是我今天學習到的: 原始碼: types_of_people = 10 x

Learn Python3 the hard way 第二天總結 命令列(2)

複製檔案 命令:cp含義:很簡單,就是把一個檔案複製成一個新檔案而已。使用 cp -r命令可以複製一些包含檔案的目錄 移動檔案 命令:mv含義:對檔案進行"rename". 檢視檔案內容 命令:less含義:這是檢視檔案內容的一種方法,它有用的地方在於,如果檔案內容有很多行,它會將其分頁,

Learn python3 the hard way》ex19 函式和變數

def cheese_and_crackers(cheese_count, boxes_of_crackers): # 定義一個有兩個引數的函式 print(f"You have {cheese_count} cheeses!") print(f

Learn python3 the hard way》ex20函式和檔案

1…seek(0)用於重置指標到檔案開頭的位置,重新開始 因為在列印完全文時游標已至文尾,要重新列印一行一行需要游標回到開頭。 2…readline()是按行來讀檔案,所以每次列印時只打印一行。 from sys import argv script,inpu

TensorFlowTFRecord資料集的製作:讀取顯示程式碼詳解

在跑通了官網的mnist和cifar10資料之後,筆者嘗試著製作自己的資料集,並儲存,讀入,顯示。 TensorFlow可以支援cifar10的資料格式, 也提供了標準的TFRecord 格式。  tensorflow 讀取資料, 官網提供了以下三種方法: 1 Feeding: 在