【问题标题】:Is it possible to combine methods? (Python, defining class methods)是否可以组合方法? (Python,定义类方法)
【发布时间】:2022-01-17 17:32:42
【问题描述】:

我目前正在学习 Python 教程,在观看了有关创建类和定义其方法的课程后,我想知道是否可以组合方法。

所以不要使用:

class Coordinates:
    def point1(self):
        print(x)

    def point2(self):
        print(y)

    def point3(self):
        print(z)

x = 5
y = 4
z = 9

Coordinates.point1(x) / Coordinates.point2(y) / Coordinates.point3(z)

我可以改用它来调用 x、y 或 z:

class Coordinates:
    def point(self):
        print(x or y or z)


x = 5
y = 4
z = 9

Coordinates.point(x/y/z)

如果我这样做,尽管我总是在终端上得到 x 的数字 5,无论我使用 x、y 还是 z 作为 self。

感谢您的任何意见:)

【问题讨论】:

  • 你想做什么? x or y or z 将评估为 x 只要 x 不是 0 每次
  • 在第二个示例中,您传递了 x/y/z 的算术结果,但没有使用它。在 point() 函数的上下文中,x、y 和 z 指的是同名的全局变量。正如@C.Nivs 所问,您尝试/希望实现什么目标?
  • 我正在尝试缩短代码,以便我可以使用 Coordinates.point(x, y or z) 根据使用的字母调用正确的坐标,而无需为 x 创建三个单独的方法, y 和 z
  • 什么是重点?什么是坐标?一般来说,圆坐标是点的属性,反之亦然。
  • @Creyze 输出中只得到 5 的原因是因为你有使用或条件。如果您的第一个条件为真,即在这种情况下是 x 它不会进一步检查条件并每次都打印(x)。感谢您加入 stackoverflow

标签: python class methods defined


【解决方案1】:

您不需要单独的函数来获取每个 x、y 和 z 值。

首先,您可以考虑通过像这样直接访问实例变量来完全没有函数:

class Coordinates:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

C = Coordinates(10, 20, 30)

print(C.x, C.y, C.z)

另一种选择是使用属性。例如:

class Coordinates:
    def __init__(self, x, y, z):
        self._x = x
        self._y = y
        self._z = z
    @property
    def x(self):
        return self._x
    @property
    def y(self):
        return self._y
    @property
    def z(self):
        return self._z

C = Coordinates(10, 20, 30)

print(C.x, C.y, C.z)

通过这种方式,您可以控制任何给定值的返回值。

你甚至可以让你的类表现得像一个字典,如下所示:

class Coordinates:
    def __init__(self, x, y, z):
        self.d = {'x': x, 'y': y, 'z': z}

    def __getitem__(self, k):
        return self.d.get(k, None)


C = Coordinates(10, 20, 30)

print(C['x'], C['y'], C['z'])

在最后一个示例中,您只有一个函数涉及获取变量,但即便如此,您也不会显式调用它

【讨论】:

  • 感谢您为我的问题提供了这么多不同的方法!看来我已经超前了,因为该教程实际上只是在教授创建课程的基础知识,而我已经在考虑提前两步,但缺乏这样做所需的工具和知识。
【解决方案2】:
class Coordinates:
    def __init__(self,x,y,z):
        self.x=x
        self.y=y
        self.z=z
    def point(self,letter):
        if letter == "x" :
           print(self.x)
        elif letter == "y" :
           print(self.y)
        elif letter == "z":
           print(self.z)


cord= Coordinates(5,4,9)
cord.point('x')

【讨论】:

  • 有一次我有一个使用 if 和 elif 的类似方法,但显然没有成功,所以谢谢你也为我清理了这部分!
【解决方案3】:

据我了解,您想从 Class 中调用一个单一方法,它应该根据什么来执行条件输入被指定为 [x 或 y 或 z],而不是为这些指定单独的方法

如果是这种情况,您可以阅读下面的如何操作

class Coordinates:
    def __init__(self,x,y,z):
        self.x_value = x # Holds the value of x coordinate when initializing the class
        self.y_valye = y # Holds the value of y ...
        self.z_value = z # Holds the value of z ...

    # The match case is only available in python 3.10 + [ you can use simple if else statements instead for below versions ]
    def eval_point(self,coordinate_axis):
        match coordinate_axis:
            case "x":
                print("Eval x coordinate function here")
            case "y":
                print("Eval y coordinate function here")
            case "z":
                print("Eval z coordinate function here")



x = 5
y = 4
z = 9

eval_coordinate = Coordinates(x,y,z) # Values should be passed in order as defined in constructor
eval_coordinate.eval_point("x") # Change the string value to whatever coordinate eval you want [example x,y,z]

【讨论】:

  • 感谢您的意见和对每个步骤的解释。这是我第一次听说 match case 语句,使用它比 if elif else 语句有什么好处吗?
  • 这里有很多优点我无法解释。最方便的一个,它比每个语句的 if-else 更容易编码。顺便说一句,如果您发现我的回答有帮助,点赞或打勾会很有帮助! .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 2011-06-09
  • 1970-01-01
  • 2011-10-02
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多