-3

so, I assign x = 1,2 Then call it

x
>>> (1,2)
OR
some_function(x) is passed the tuple (1,2)

What method or configuration in the class is doing that?

I ask because I want to create a class "myclass" and when I call an instance the instance should return the values assigned to it

When I asked this question another way the responses thus far have been along the lines of create a method...but I don't want to use x.vals to get the values from x, I just want to use x.
How is this done ? ...and what I really really want is an example of the class code so I can put it in my class :)

Oh, better tag suggestions are MOST welcome. Not knowing the jargon puts me at a real disadvantage here

DaftVader
  • 105
  • 1
  • 11
  • Neither ``x`` nor ``print(x)`` call ``x``. Do you want to set a class' default representation? – MisterMiyagi Feb 09 '21 at 20:26
  • If you hope to learn python by asking on SO - you'll get disapointed fast. This is not the purpose of SO. Google python tutorials, read the documentation about classes etc. – Patrick Artner Feb 09 '21 at 20:29
  • If you want ``x`` to *literally evaluate to* ``(1, 2)`` that is not possible, and in fact not even well-defined. ``x`` is just some name for a ``myclass`` – just like ``self`` in a method. If any reference to an instance immediately unwraps the instance, no instance can exist. – MisterMiyagi Feb 09 '21 at 20:30
  • @PatrickArtner yeah I'm noticing that effect. Unfortunately you have to know what you want to know with correct jargon before you can find it. Searching for green thing in tree might return leaves but you were actually looking for frogs...yeah that's a huge time suck. Additionally in my searches for class construction examples, and python tutorials on classes none show this example – DaftVader Feb 09 '21 at 21:33
  • @MisterMiyagi Googling " class' default representation" suggests no. I don't want the string, I want the tuple returned. – DaftVader Feb 09 '21 at 21:36
  • If you want ``x`` to be a tuple, what expression do you want to be a myclass? – MisterMiyagi Feb 09 '21 at 21:37

2 Answers2

1

Objects have two methods to control their display: __str__ and __repr__.

__str__ is the "user" representation returning the string you want to see when you print it or call str(obj).

__repr__ is the "programmer" representation. It should return a string useful for debugging. repr(obj) calls this method. Ideally, eval(repr(obj)) should re-create the object, but it isn't required.

For your x=1 example, the int class has these methods defined to return a string of the decimal digits of the integer.

For your example:

class myclass:

    def __init__(self,a,b):
        self.a = a
        self.b = b

    def __repr__(self):
        return f'myclass({self.a}, {self.b})'

    def __str__(self):
        return f'({self.a}, {self.b})'

m = myclass(1,2)
print(m)
print(repr(m))
n = eval(repr(m))
print(repr(n))
(1, 2)                   # from __str__
myclass(1, 2)            # from __repr__
myclass(1, 2)            # eval(repr(obj))

Also, str(obj) or print(obj) calls __repr__ if __str__ isn't defined.

Ref: Basic Customization

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Hi Mark, thank you for your response. I'm not trying to get a string representation of the values though, rather the tuple. some_function(x) is not passed a string, rather the tuple. How is that achieved ? – DaftVader Feb 09 '21 at 21:30
  • @DaftVader Just add a method to the class to return the tuple. For example: `some_function(x.get_tuple())`. – Mark Tolonen Feb 09 '21 at 21:32
  • x = myclass(1,2) ...some_function(x) should not require some_function(x.get_tuple()) How do I define my class to return that tuple when the instance is called ? – DaftVader Feb 09 '21 at 21:49
  • @DaftVader You cant. `x` is a `myclass` object, not a `tuple` object. You need a converter function. – Mark Tolonen Feb 09 '21 at 22:08
0

I hope I understood well your question. Try this:

>>> x = (1,2)
>>> def some_function(value):
...    print(type(value))
...    print(value[0])
...    print(value[1])
...
>>> some_function(x)
<type 'tuple'>
1
2

And as you mentioned you wanted to call a class method, here's the method version:

>>> x = (1,2)
>>> class MyClass:
...    def some_method(self, value):
...        print(type(value))
...        print(value[0])
...        print(value[1])
...
>>> my_object = MyClass()
>>>
>>> my_object.some_method(x)
<type 'tuple'>
1
2
>>>
Gerard Yin
  • 1,283
  • 1
  • 12
  • 24