0

I have answered my own question - see answer below
I'm writing a class, and I want this behavior:

a = f(10,20)
some_funct(a.row) # some_function is given 10
some_funct(a.col) # some_function is given 20
some_funct(a)     # some_function is given a tuple of 10, 20  <-- THIS ONE :)

The last behavior is stumping me. I have not seen any examples that cover this.

Thus far:

class f(object):
    """Simple 2d object"""
    row: int
    col: int

    def __init__(self, row, col):
        self.row = row
        self.col = col

Explictly I do not want another method, say, self.both = row, col. I just want to 'call' the instance

I'm new to classes, so any improvements are welcome. Properties, setters, getters etc.

EDIT 1: Replaced "print" with "some_function" in the question, and modified title

DaftVader
  • 105
  • 1
  • 11

4 Answers4

0

You can do like this

class f(object):
    """Simple 2d object"""
    row: int
    col: int

    def __init__(self, row, col):
        self.row = row
        self.col = col

    def __str__(self):
        return f"row = {row}, col = {col}"

and print like this

a = f(10,20)

print(a)     # row = 10, col = 20
  • Thank you Hillal, this does not suit my needs but my question was confusing. I have modified my question to remove the confusing print call. – DaftVader Feb 04 '21 at 07:57
0

From python 3.7 dataclasses have been introduced and their goal is to create classes that mainly contains data. Dataclasses comes with some helper function that extract the class attributes dict/tuples. e.g.

from dataclasses import dataclass,asdict,astuple
@dataclass
class f:
 x: int
 y: int

f_instance = f(10,20)
asdict(f_instance) # --> {'x': 10, 'y': 20}
astuple(f_instance) # -> (10,20)

EDIT I : Another technique would be to use namedtuple e.g.:

from collections import namedtuple
f = namedtuple('p',['row','col'])
a =f(10,20)
a.row #-> 10
a.col #-> 20
  • print(f_instance) returns . I need it to return (10,20) – DaftVader Feb 04 '21 at 08:41
  • As written you need to use the astuple function i.e. `astuple(f_instance)` and this will return `(10,20)` – Adriano franci Feb 04 '21 at 10:10
  • Perhaps an alternative question. If I assign x = 1 I can simply print(x) and it works the way I want in my question. How is the 'x' class implemented so I can use that in my class to achieve the same functionality. – DaftVader Feb 04 '21 at 10:42
  • 1
    Maybe `named tuple` should work ? `from collections import namedtuple f = namedtuple('p',['row','col']) a =f(10,20) a.row #--> 10 a.col # --> 20` – Adriano franci Feb 04 '21 at 12:22
  • Could you show the complete written class please – DaftVader Feb 09 '21 at 20:00
  • I have edited my answer, everything is there. You create a named tuple assigning a property name and then you can create instances of this named tuple and access its properties e.g. `(a.row,a.col) = (10,20)` – Adriano franci Feb 10 '21 at 13:06
  • Please see my answer which gives tuple behavior. Thank you for your efforts
    can't up vote my own answer though :/
    – DaftVader Feb 11 '21 at 04:26
0

This might help


class f(object):
    """Simple 2d object"""
    row: int
    col: int

    def __init__(self, row, col):
        self.row = row
        self.col = col

    def some_funct(self):
        return (self.row, self.col)

You can access like

a = f(10,20)
a.some_funct() # (10, 20)
# or
row, col = a.some_funct()
  • ah yes, I'm currently using this method solution of def some_funct, I find it somewhat ugly in my code. Perhaps an alternative question. If I assign x = 1 I can simply print(x) and it works the way I want in my question. How is the 'x' class implemented so I can use that in my class to achieve the same functionality – DaftVader Feb 04 '21 at 10:40
0
class f(tuple):
    """Simple 2d object"""
    def __new__(cls, x, y):
        return tuple.__new__(f, (x, y))

    def __init__(self, x, y):
        self.col = x
        self.row = y

foo = f(1,2)
print(foo.col)
>>>1
print(foo.row)
>>>2
print(foo)
>>>(1, 2)

Importantly:
If you want it to behave like a tuple then make it a subclass of tuple.

Much stuffing around but stumbled upon an external site which gave me clues about the keywords to search on here. The SO question is here but I have modified that answer slightly.
I'm still a little confused because the other site says to use new in the init as well but does not give a clear example.

DaftVader
  • 105
  • 1
  • 11