python - 如何在子类中使用父类的属性?
问题描述
我是 Python 中 OOP 的初学者。这是我的代码块。
#%% IMPORTING MODULES
import numpy as np
import netCDF4 as nc4
import datetime as dt
#%% RAINFALL CLASS
class Rainfall():
date_zero = dt.date(1901,1,1)
#Initialise the class attributes
def __init__(self,path):
self.path = path
self.dataset = nc4.Dataset(self.path, mode='r')
self.lon = self.dataset.variables['LON'][:]
self.lat = self.dataset.variables['LAT'][:]
self.time = self.dataset.variables['TIME'][:]
# self.rf = self.dataset.variables['RAIN'][:]
self.date = np.arange(dt.datetime(1979,1,1), dt.datetime(2019,1,1), dt.timedelta(days=1)).astype(dt.datetime)
self.index_jjas = []
for i,val in enumerate(self.date):
if val.month >= 6 and val.month<=9:
self.index_jjas.append(i)
self.jjas_rf = self.dataset.variables['RAIN'][self.index_jjas]
self.nonzero = self.nonzero_jjas()
self.sorted_rf = self.sorted_list(self.nonzero)
self.threshold = self.p95(self.sorted_rf)
self.val_abv_threshold = np.ma.masked_less(self.nonzero, self.threshold)
self.abv_threshold = np.ma.MaskedArray.count(self.val_abv_threshold, axis=0)
self.clim = self.simpleprob(self.abv_threshold, len(self.index_jjas))
#Method to find the non zero precipitation
def nonzero_jjas(self):
return np.ma.masked_less(self.jjas_rf, 0.2)
#Method to sort the non zero precipitation array
def sorted_list(self, nz):
return np.ma.MaskedArray.filled(np.ma.sort(nz, axis=0), np.nan)
#Method to obtain the 95th percentile for threshold value to identify extremes
def p95(self,ns):
return np.nanpercentile(ns, 95, axis=0)
#Method to obtain the probabiltiy
def simpleprob(self, a1, a2):
return np.divide(a1, a2)
#Method to identify ExtremeDays in Waves
def extr(self, a1, a2, data, clim):
m1 = a1.mask
m2 = a2.mask
m3 = m1 | m2
count = np.ma.MaskedArray.count(a2, axis=0)
data_new = np.ma.masked_where(m3, data)
data_count = np.ma.MaskedArray.count(data_new, axis=0)
data_prob = np.divide(data_count, count)
prob_diff = data_prob - clim
return (data_new, data_count, data_prob, prob_diff)
#%% Waves Class
class Waves(Rainfall):
#Initialise the class attributes
def __init__(self, path1, path2):
self.olr_path = path1
self.mvr_path = path2
self.olr_dataset = nc4.Dataset(self.olr_path, mode='r')
self.mvr_dataset = nc4.Dataset(self.mvr_path, mode='r')
self.date = np.arange(dt.datetime(1979,1,1), dt.datetime(2019,1,1), dt.timedelta(days=1)).astype(dt.datetime)
self.index_jjas = []
for i,val in enumerate(self.date):
if val.month >= 6 and val.month<=9:
self.index_jjas.append(i)
self.olr = self.olr_dataset.variables['olr'][self.index_jjas]
self.mvr = self.mvr_dataset.variables['var'][self.index_jjas]
self.mn_mvr = np.nanmean(self.mvr, axis=0)
self.std_olr = np.nanstd(self.olr, axis=0)
self.active = self.active_days()
self.dry = self.dry_days(self.active)
self.wet = self.wet_days(self.active)
self.ext_dry = self.extr(self.val_abv_threshold, self.dry, self.olr, self.clim)
self.ext_wet = self.extr(self.val_abv_threshold, self.wet, self.olr, self.clim)
#Method to find the active days
def active_days(self):
return np.ma.masked_where(np.ma.getmask(np.ma.masked_less(self.mvr, self.mn_mvr)), self.olr)
#Method to find the dry days
def dry_days(self, act1):
return np.ma.masked_less_equal(act1, (0.5*self.std_olr))
#Method to find the wet days
def wet_days(self, act1):
return np.ma.masked_greater_equal(act1, (-0.5*self.std_olr))
#%% Create Objects and other attributes
rain = Rainfall(path='rf_1979_2018.nc')
mjo = Waves(path1='/home/anik3t/Documents/Data/olr/0.25degfiles/mjo_final.nc', path2= '/home/anik3t/Documents/Data/olr/mjo_var.nc')
我需要从无法访问的子 Waves 类中的父 Rainfall 类访问 val_abv_threshold。运行代码会给我以下错误消息:
Traceback (most recent call last):
File "/home/anik3t/.config/spyder-py3/test_run1.py", line 109, in <module>
mjo = Waves(path1='/home/anik3t/Documents/Data/olr/0.25degfiles/mjo_final.nc', path2= '/home/anik3t/Documents/Data/olr/mjo_var.nc')
File "/home/anik3t/.config/spyder-py3/test_run1.py", line 91, in __init__
self.ext_dry = self.extr(self.val_abv_threshold, self.dry, self.olr, self.clim)
AttributeError: 'Waves' object has no attribute 'val_abv_threshold'
我认为 Waves 类无法从父类访问所需的属性。我试图使用 super() 函数,但我不确定它应该如何在这里使用。
解决方案
您需要致电:
super().__init__(<some path>)
从Waves
类的构造函数调用它的父类的构造函数。否则,该代码将不会被执行。
推荐阅读
- python-3.x - “解除分配的字节数组对象已导出缓冲区”是什么意思
- gulp - 使用 gulp 构建我的项目抛出错误,如何解决它
- reactjs - 将来自多个数组的 API 结果合并到 React.js 组件中的单个数组中
- angular - 如何从 ngb datepicker 中删除年份箭头
- scipy - 如何获得 Delaunay 三角剖分的“边界”?
- c# - 如何在网格中显示 ListView 项目
- r - 用二进制值格式化 data.frame
- arrays - 使用高效的 insert/findKthSmallest 操作实现数据结构
- r - ggplot 中的 facet 函数如何用于创建直方图以可视化数据集中所有变量的分布?
- rest - 使用 Spoke of Integration Hub 设置 DocuSign 与 ServiceNow 集成的问题