【问题标题】:Problem with recognising attributes inside a function识别函数内部属性的问题
【发布时间】:2020-07-16 06:23:57
【问题描述】:
#!/usr/bin/env python3
#!/config.py
_UNIGENE_DIR = "/data/PROGRAMMING/assignment5"
_UNIGENE_FILE_ENDING = "unigene"

    def get_unigene_directory (self,_UNIGENE_DIR) :
        """Returns the absolute path"""
        self._UNIGENE_DIR = _UNIGENE_DIR
        print(self._UNIGENE_DIR)

   def get_uigene_extension(self,_UNIGENE_FILE_ENDING) :
       """Returns the extension of the file"""
       self._UNIGENE_FILE_ENDING = _UNIGENE_FILE_ENDING
       print(self_UNIGENE_FILE_ENDING)

所以,当我在我的 bash 的 python 环境中运行以下命令时:

      file = "/".join( (config.get_unigene_directory(), host, gene + "." + 
      config.get_unigene_extension() ) )

我收到以下错误:

     TypeError: get_unigene_directory() missing 2 required positional arguments: 'self' and 
     '_UNIGENE_DIR'.May I know whats the reason for this error?

【问题讨论】:

    标签: python bash function parameters shebang


    【解决方案1】:

    我想你不知道函数在 Python 中是如何工作的。我建议你看看这个tutorial

    基本上,如果你声明一个带有一个或多个参数的函数,你需要在调用它时传递它们。

    另外,你不能在课堂之外使用self。这个关键字指的是方法所在的实际类。

    【讨论】:

    • 对,但是当我像这样传递它时: _UNIGENE_DIR = "/data/PROGRAMMING/assignment5" _UNIGENE_FILE_ENDING = "unigene" def get_unigene_directory () : """返回绝对路径""" return (_UNIGENE_DIR ) def get_uigene_extension() : """返回文件的扩展名""" return (_UNIGENE_FILE_ENDING) ,
    最近更新 更多