我有一個類用于處理三種型別的資料結構。在這個類中,我有很多繪圖方法,這取決于加載到類中的資料型別。在查看類屬性時,有沒有辦法隱藏不屬于加載的資料結構的方法?
例子:
class data_reader():
def __init__(self):
self.load_data()
self.data_type()
self.common_method_1()
self.common_method_2()
def load_data(self):
# Loads the data
def data_type(self):
# Figures out which of the three data structures we have
def common_method_1(self):
# A method common for all data structures
def common_method_2(self):
# Another method common for all data structures
def plot_data_1(self):
# Plotting function for data structure 1
def plot_data_2(self):
# Plot function for data structure 2
def plot_data_3(self):
# Plot function for data structure 3
if __name__ == "__main__":
a = data_reader()
a.plot_data_1()
當我檢查類的方法時,我可以看到所有繪圖函式。如果我加載資料結構 1,我可以隱藏其他兩個繪圖功能嗎?
我試圖做一些內部函式,但它并沒有成為類外的可呼叫方法。
感謝您的任何意見。
uj5u.com熱心網友回復:
不應隱藏與實際物件無關的方法。他們應該在那里。因此,不要為不同的資料型別創建一個“通用”物件,而是將公共邏輯提取到父類中并繼承到定義特定于資料型別的行為的特定子類。
當然,這需要您在運行前了解資料型別。如果您只能在實際加載資料時決定,我建議實作工廠模式 - 一個單獨的類,用于處理決定創建哪個物件的邏輯。
class data_reader():
def __init__(self, data):
self.common_method_1()
self.common_method_2()
def common_method_1(self):
pass
def common_method_2(self):
pass
class data_reader_1(data_reader):
def plot_data_1(self):
pass
# Plotting function for data structure 1
class data_reader_2(data_reader):
def plot_data_2(self):
pass
# Plot function for data structure 2
class data_reader_factory():
def load_data(self):
self.data = 42
# Loads the data
def data_type(self):
self.type = "1"
# Figures out which of the three data structures we have
def create_reader(self):
if self.type == "1":
return data_reader_1(self.data)
elif self.type == "2":
return data_reader_2(self.data)
# Creates an object of correct type
if __name__ == "__main__":
a = data_reader_factory().create_reader()
a.plot_data_1()
uj5u.com熱心網友回復:
我的方法取決于您的各種資料結構有多“不同”:
V1: 差別不大,例如,嵌套串列與 numpy 陣列。在這種情況下,我建議您撰寫不同的資料加載函式,這些函式總是將資料轉換為通用格式,例如:
def load_list_data(self, data):
# reads a list and converts it to a numpy array
def load_npy_array_data(self, data):
# reads a numpy array which does not need to be converted
def plot_data(self):
# plots a numpy array
V2: 資料差異很大,例如點云與影像。然后使用兩個子類和一個工廠方法:
class data_reader():
def __init__(self):
self.load_data()
self.data_type()
self.common_method_1()
self.common_method_2()
def load_data(self):
# Loads the data
def data_type(self):
# Figures out which of the three data structures we have
def common_method_1(self):
# A method common for all data structures
def common_method_2(self):
# Another method common for all data structures
class img_data_read(data_reader):
def plot_data(self):
# Plotting function for data structure 1
class pointcloud_data_read(data_reader):
def plot_data(self):
# Plotting function for data structure 1
def data_reader_factory(data):
if isinstance(data, IMG_DATA_TYPE):
return img_data_read()
elif isinstance(data, POINTCLOUD_DATA_TYPE):
return pointcloud_data_read()
然后你可以這樣做:
if __name__ == "__main__":
a = data_reader_factory()
a.plot_data()
uj5u.com熱心網友回復:
根據以下評論matszwecja:
將所有常用方法放在基類中data_reader,然后創建此類覆寫方法的子類plot_data。基類不再需要了解各種型別的繪圖。plot_data將根據實際類是什么來呼叫正確的方法。這是面向物件編程的基本技術。例如,參見面向物件編程:Python 中的多型性。
from abc import ABC, abstractmethod
# This is an abstract class: method plot_data
# must be overriden in a subclass:
class data_reader(ABC):
def __init__(self):
self.load_data()
self.common_method_1()
self.common_method_2()
def load_data(self):
# Loads the data
...
def common_method_1(self):
# A method common for all data structures
...
def common_method_2(self):
# Another method common for all data structures
...
# This method must be overridden in a subclass:
@abstractmethod
def plot_data(self):
pass
class data_reader_1(data_reader):
def plot_data(self):
# Plotting function for data structure 1
...
class data_reader_2(data_reader):
def plot_data(self):
# Plotting function for data structure 2
...
class data_reader_3(data_reader):
def plot_data(self):
# Plotting function for data structure 3
...
if __name__ == "__main__":
a = data_reader_1() # A specific subclass
a.plot_data()
如果load_data必須對每個子類都是特定的,則將其設為基類中的抽象方法(意味著它必須在子類中被覆寫),然后為每個子類定義它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529548.html
標籤:Python功能班级方法
