Skip to main content
 首页 » 编程设计

python之如何将输入连接到函数

2025年05月04日18fff_TT

我有一些setter函数:

      @images_path.setter 
      def images_path(self, *images_path: tuple): 
         self.__images_path =  
           os.path.abspath(os.path.join(os.pardir,'BossGame', 'Resources','images')) 

我想传递包含以下内容的输入:'BossGame'、'Resources'、'images' 在 os.path.join 函数的输入 images_path 中

 class Nature(object): 
 
   def __init__(self): 
 
    self.images_path = ['BossGame', 'Resources', 'images'] 
    self.sound_path = ['BossGame', 'Resources', 'music'] 
 
    pass 
 
   @property 
   def images_path(self)->str: 
       return self.__images_path 
 
   @images_path.setter 
   def images_path(self, *images_path: tuple): 
       self.__images_path =  
           os.path.abspath(os.path.join(os.pardir,images_path)) 

错误:

      TypeError: join() argument must be str or bytes, not 'list' 

请您参考如下方法:

def images_path(self, path_seq): 
   self.__images_path =  
       os.path.abspath(os.path.join(os.pardir, *path_seq)) 

请注意,您已将 images_path 用作变量名和函数名。这是非常糟糕的做法。