Y
Y
Yura Khlyan2015-06-03 12:05:14
Python
Yura Khlyan, 2015-06-03 12:05:14

How to create a class with an indefinite number of variables?

Good time of the day.
I have this problem: you need to create a class - Vector, which can be of any dimension. And for him a couple of methods. I have a hitch with the initiation, and the presentation in the text version.

a = Vector(1,2,3)
str(a) == '(1,2,3)' #так должень быть представлен

Here's what I tried myself, but alas (
class Vector:
  def __init__(self, *args):
    for arg in args:
      setattr(self, str(arg), arg)
      
  def __str__(self):
      return "({})".format(",".join(args))

P.S. Sorry for my bad english.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2015-06-03
@MAGistr_MTM

Maybe something like this

class Vector:
  def __init__(self, *args):
    self.data = args
      
  def __str__(self):
      return "({})".format(",".join(str(e) for e in self.data))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question