我正在尝试将输入目录路径从一个单独的脚本传递到主模块“ qc”中。
我第一次尝试在qc模块中将参数从父级传递给子级:
import os
class Data:
def __init__(self, in_dir):
# get all inputs
self.in_dir = in_dir # get input directory
# get files from input directory
self.files = os.listdir(self.in_dir) # get file list
class LAS(Data):
"""Takes in log files from parent class Data's file list"""
def __init__(self):
# inherit the directory from Data parent
super().__init__(LAS, in_dir)
# target only the .las files in the directory
ext_las = ['.las', '.LAS', '.Las', '.LAs']
self.lasfiles = [lasfile for lasfile in self.files if any(match in lasfile for match in ext_las)]
def out_las(self):
return self.lasfiles
当我从单独的脚本中调用类时,它看起来像这样:
in_dir = 'C:\\......directory path...'
Data(in_dir=in_dir)
output = LAS().out_las()
哪个会引发错误:
File "C:\......", line 62, in __init__
super().__init__(LAS, in_dir)
NameError: name 'in_dir' is not defined
我不知道如何正确调用该父类的输入。
我做错了什么?
答案 0 :(得分:1)
很难为您指明正确的方向,但是看起来您希望子类继承自父类的实例-事实并非如此。
以下内容可能会达到您的预期效果,希望对您有所帮助。
class LAS(Data):
"""Takes in log files from parent class Data's file list"""
def __init__(self, in_dir):
# inherit the directory from Data parent
super().__init__(in_dir)
# target only the .las files in the directory
ext_las = ['.las', '.LAS', '.Las', '.LAs']
self.lasfiles = [lasfile for lasfile in self.files if any(match in lasfile for match in ext_las)]
def out_las(self):
return self.lasfiles
las = LAS(in_dir='C:\\......directory path...')
output = las.out_las()