How Do I Use Multiple Values in a Tuple for a Function Argument That Takes One Argument
Question:
I’m trying to use a fuction that does math that takes the inputs of 2 sets of tuples and use the index of the tuples to do the math, versus having 4 variables.
class numbers():
def points(self, x: float, y: float):
self.x = x
self.y = y
return self.x, self.y
def maths(self, *args):
foobar1 = self.x, self.y
foobar2 = args[0], args[1]
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers()
num2 = numbers()
num1.points(4, 10)
num2.points(5, 11)
num2.maths(num1.points)
What I would like is the argument in def maths()
to take both points from num1.points
(4, 10) so that I can use the two points in a similar manner to num2.x
and num2.y
Answers:
I would recommend making a constructor init to initialize your class variables x and y. You can remove the points function and instead of passing a tuple for the maths function, if you plan to pass the x and y variables of another numbers object, you can pass another number object instead.
class numbers():
def __init__(self, x, y):
self.x = x
self.y = y
def maths(self, otherNumberObj):
foobar1 = self.x, self.y
foobar2 = otherNumberObj.x, otherNumberObj.y
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers(4, 10)
num2 = numbers(2, 5)
print(num2.maths(num1))
If you really want to keep the function maths as passing a tuple into it, you could do something like this:
class numbers():
def __init__(self, x, y):
self.x = x
self.y = y
def maths(self, someTuple):
foobar1 = self.x, self.y
foobar2 = someTuple[0], someTuple[1]
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers(4, 10)
num2 = numbers(2, 5)
print(num2.maths((num1.x, num1.y)))
First you need to update the maths
method to let foobar2
accept a tuple.
def maths(self, *args):
foobar1 = self.x, self.y
# foobar2 = args[0], args[1]
foobar2 = args[0]
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
Then you can pass a tuple to maths
.
num2.maths((num1.x, num1.y))
Another way to do this is to add another function to create the tuple for you.
def get_point(self):
return (self.x, self.y)
Then do
num2.maths(num1.get_point())
That being said, having *args
and the assignment of the variables to foobar1/2
is unnecessary and can be simplified to the following:
def maths(self, t2):
return self.x - t2.x + self.y - t2.y
Further more it would also be better to initialize the points when you create the numbers
object, so full refactored it would look like:
class Numbers():
def __init__(self, x, y):
self.x = x
self.y = y
def maths(self, t2):
return self.x - t2.x + self.y - t2.y
def get_point(self):
return (self.x, self.y)
num1 = Numbers(4, 10)
num2 = Numbers(5, 11)
num2.maths((num1.x, num1.y))
num2.maths(num1.get_point())
I’m sure there is a better way, but this got it working (very similar to Abdul Barkat and BWallDev):
class numbers():
def points(self, x: float, y: float):
self.x = x
self.y = y
return self.x, self.y
def maths(self, other):
foobar1 = self.x, self.y
foobar2 = other.x, other.y
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers()
num2 = numbers()
num1.points(5, 11)
num2.points(10, 22)
print(num2.maths(num1))
>>> 16
Better to have a class to support the points then the maths() function of the numbers class can take references to a points class. Something like this:
class numbers:
def __init__(self):
pass
def maths(self, p1, p2):
return p1._x - p1._y + p2._x - p2._y
class points:
def __init__(self, x, y):
self._x = x
self._y = y
p1 = points(4, 10)
p2 = points(5, 11)
print(numbers().maths(p1, p2))
I’m trying to use a fuction that does math that takes the inputs of 2 sets of tuples and use the index of the tuples to do the math, versus having 4 variables.
class numbers():
def points(self, x: float, y: float):
self.x = x
self.y = y
return self.x, self.y
def maths(self, *args):
foobar1 = self.x, self.y
foobar2 = args[0], args[1]
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers()
num2 = numbers()
num1.points(4, 10)
num2.points(5, 11)
num2.maths(num1.points)
What I would like is the argument in def maths()
to take both points from num1.points
(4, 10) so that I can use the two points in a similar manner to num2.x
and num2.y
I would recommend making a constructor init to initialize your class variables x and y. You can remove the points function and instead of passing a tuple for the maths function, if you plan to pass the x and y variables of another numbers object, you can pass another number object instead.
class numbers():
def __init__(self, x, y):
self.x = x
self.y = y
def maths(self, otherNumberObj):
foobar1 = self.x, self.y
foobar2 = otherNumberObj.x, otherNumberObj.y
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers(4, 10)
num2 = numbers(2, 5)
print(num2.maths(num1))
If you really want to keep the function maths as passing a tuple into it, you could do something like this:
class numbers():
def __init__(self, x, y):
self.x = x
self.y = y
def maths(self, someTuple):
foobar1 = self.x, self.y
foobar2 = someTuple[0], someTuple[1]
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers(4, 10)
num2 = numbers(2, 5)
print(num2.maths((num1.x, num1.y)))
First you need to update the maths
method to let foobar2
accept a tuple.
def maths(self, *args):
foobar1 = self.x, self.y
# foobar2 = args[0], args[1]
foobar2 = args[0]
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
Then you can pass a tuple to maths
.
num2.maths((num1.x, num1.y))
Another way to do this is to add another function to create the tuple for you.
def get_point(self):
return (self.x, self.y)
Then do
num2.maths(num1.get_point())
That being said, having *args
and the assignment of the variables to foobar1/2
is unnecessary and can be simplified to the following:
def maths(self, t2):
return self.x - t2.x + self.y - t2.y
Further more it would also be better to initialize the points when you create the numbers
object, so full refactored it would look like:
class Numbers():
def __init__(self, x, y):
self.x = x
self.y = y
def maths(self, t2):
return self.x - t2.x + self.y - t2.y
def get_point(self):
return (self.x, self.y)
num1 = Numbers(4, 10)
num2 = Numbers(5, 11)
num2.maths((num1.x, num1.y))
num2.maths(num1.get_point())
I’m sure there is a better way, but this got it working (very similar to Abdul Barkat and BWallDev):
class numbers():
def points(self, x: float, y: float):
self.x = x
self.y = y
return self.x, self.y
def maths(self, other):
foobar1 = self.x, self.y
foobar2 = other.x, other.y
return foobar1[0] - foobar2[0] + foobar1[1] - foobar2[1]
num1 = numbers()
num2 = numbers()
num1.points(5, 11)
num2.points(10, 22)
print(num2.maths(num1))
>>> 16
Better to have a class to support the points then the maths() function of the numbers class can take references to a points class. Something like this:
class numbers:
def __init__(self):
pass
def maths(self, p1, p2):
return p1._x - p1._y + p2._x - p2._y
class points:
def __init__(self, x, y):
self._x = x
self._y = y
p1 = points(4, 10)
p2 = points(5, 11)
print(numbers().maths(p1, p2))