设置Python类属性的正确方法

时间:2018-04-18 18:57:12

标签: python python-3.x class oop

所以我试图学习如何正确设置Python类的Class属性,并想知道如何使用类函数。

我的初学者教会我这样做:

class MyClass:
    """MyClass class instance"""
    def __init__(self, project, version, command):
        self.project = project
        self.__version = version
        self.__command = command


    """Get version used"""
    def getVersion(self):
        return self.__version

    """Get command executed"""
    def getCommand(self):
        return self.__command

    """Set version used"""
    def setVersion(self, version):
        self.__version = version

这很简单,但这意味着我在实例化MyClass时已经知道了属性的值,因为我传递它们来创建我的对象,或者如果我不通过它们,我会使用我的创建MyClass对象后setVersion(version)的初学者方式。在我的搜索中,我阅读了this,它引导我阅读properties,并让我意识到我不应该在Python中使用getter / setter而是使用属性。它是否正确?

但是,我想知道它是否是使用函数设置一些MyClass实例属性(项目,版本,命令)的Pythonic(或者甚至是正确的)。我想这样做是因为我希望这个类只通过传递1个参数来完成从特定文件中查找这些值的工作。想到这一点,但认为这可能是错误的:

class MyClass:
    """MyClass class instance"""
    def __init__(self, project):
        self.project = project
        self._version = self.version()
        self._command = self.command()

    """Get version used"""
    def version(self):
        ...
        use `project` attribute here to get `version` from file...
        ...
        return version

    """Get command executed"""
    def command(self):
        ...
        use `project` attribute here to get `command` from file...
        ...
        return command

通过调用类函数甚至是设置实例变量的逻辑方法吗?

我应该在某些方面使用属性吗?

我应该在MyClass之外创建一个找到这些值的函数,而不是返回一个MyClass实例吗?

是否有更合适的Pythonic方式?

我基本上想要通过使用1个传递属性实例化来创建一个包含所有额外信息(版本,命令等)的对象。

提前谢谢!

2 个答案:

答案 0 :(得分:6)

有很多方法可以解决这个问题,但这就是我通常的做法:

class MyClass:
    """MyClass class instance"""
    def __init__(self, project, version, command):
        self.project = project
        self.version = version
        self.command = command

    @classmethod
    def from_file(cls, project):
        """ 
        Alternate constructor that takes a project 
        and extracts the other attributes from a file
        """
        #... some code to use `project` 
        #... and get `version` and `command` from file
        return cls(project, version, command)

# usage
m = MyClass.from_file('project01')

classmethod是一个装饰器,它使一个方法能够直接在类中调用,而不是实例。 然后该方法将自动将类作为第一个参数。 cls只是一个表示类的名称,就像我们对实例使用self但我们可以使用任何名称。 __init__中的主对象构造函数采用完整参数,但新的from_file类方法作为替代构造函数, 通过创建类的实例并将其返回。

请注意,这不是一个明确的答案,而只是建议采用pythonic方法来解决它。

答案 1 :(得分:-1)

  1. 通过在@property中使用关键字变量,您可以灵活地在实例化时仅传递一个变量。
  2. 使用诸如class MyClass: def __init__(self, project, version=None, command=None): self.project = project self.__version = version self.__command = command @property def version(self): return self.__version @version.setter def version(self, val): self.__version = val @property def command(self): return self.__command @command.setter def command(self, val): self.__command = val 和@ bar.setter之类的装饰器可以访问和修改相关属性。而且我相信它也是一种非常Pythonic的方式。
  3. 以下是我的方法。

    String testString = "text\r\nMoreText\r\n Hello There!\nHow Are You?\r\n I'm Fine \r\n \r\n "
    String configuredString = testString.replaceAll("(\\r\\n){1}[\\s]+\\b", " ");