简体   繁体   English

是否可以组合方法? (Python,定义 class 方法)

[英]Is it possible to combine methods? (Python, defining class methods)

I'm currently woring through a Python tutorial and after watching the lesson about creating a class and defining its methods I was wondering if it is possible to combine methods.我目前正在学习 Python 教程,在观看了有关创建 class 并定义其方法的课程后,我想知道是否可以组合方法。

So instead of using:所以不要使用:

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)

I could instead use this to call for either x, y or 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)

If I do that though I always get x's number 5 on the terminal no matter if I use x, y or z as self.如果我这样做,尽管我总是在终端上得到 x 的数字 5,无论我是否使用 x、y 或 z 作为 self。

Thanks for any input:)感谢您的任何输入:)

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')

Dude your question is not well defined :(. What are you expecting to be the output?伙计,您的问题没有明确定义:(。您期望 output 是什么?

As far as i understood, you want to call a single method from the Class which should execute conditions based on what input is specified [x or y or z] instead of having separate methods for those.据我了解,您想从Class调用单个方法,该方法应根据指定的输入 [x 或 y 或 z] 执行条件,而不是为这些方法设置单独的方法

If that's the case, You can read below on how to do it :)如果是这种情况,您可以阅读下文了解如何操作:)

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]

Happy coding:)快乐编码:)

You don't want separate functions for getting each of the x, y & z values.您不需要单独的函数来获取每个 x、y 和 z 值。

Firstly you could consider having no functions at all by directly accessing the instance variables like this:首先,您可以通过直接访问这样的实例变量来考虑根本没有函数:

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)

Another option would be to use properties.另一种选择是使用属性。 For example:例如:

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)

In this way you can exert control over what gets returned for any given value.通过这种方式,您可以控制任何给定值的返回值。

You could even make your class behave like a dictionary as follows:你甚至可以让你的 class 表现得像一个字典,如下所示:

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'])

In this last example you have just one function involved in the acquisition of a variable but even then you don't call it explicitly在最后一个示例中,您只有一个 function 参与了变量的获取,但即便如此您也没有明确调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM