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.
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:
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.
Thanks for any input :)
CodePudding user response:
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')
CodePudding user response:
Dude your question is not well defined :(. What are you expecting to be the 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.
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 :)
CodePudding user response:
You don't want separate functions for getting each of the x, y & z values.
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 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