0

I have the following problem.

class Operacoes(object):

    def __init__(self, *args):
        self.__numero = args

    def get_numero(self) -> int:
        return self.__numero

x = Operacoes(10).get_numero()
print(x)
#(10,)

This is making me very intrigued, why does anyone know how to tell me?

args, should return an int in case number 10, but a Tuple is returning.

  • 2
    10 is passed as a tuple as you have used `*args` – Ceres Mar 19 '21 at 16:01
  • 3
    I don't see the point of using `*args` here. Just do `def __init__(self, num):` and it'll have the desired output. – M-Chen-3 Mar 19 '21 at 16:01
  • Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Mike Scotty Mar 19 '21 at 16:11

2 Answers2

1

It is because when you call the get_numero() method, self.__numero is returned. And when looking at self.__numero in __init__(), it is returning args, which is always a Tuple, even if you have only 1 argument.

Cheukting
  • 264
  • 1
  • 6
0

Let's look closer at the syntax. You assigned

*args = 10

The star is really important here because it does unpacking of a tuple/list. By Python specification args is a tuple, even if it contains a single element.

Then, self.__numero stores a tuple. When you retrieve it and print it, it is still a tuple.

Kate Melnykova
  • 1,863
  • 1
  • 5
  • 17