1

I'm sick of typing

class A:
    def __init__(self, x, y, z):
        self.x,self.y,self.z = x,y,z

It just feel like redundant code. Is there a way to say "these variables that I pass in will become fields with the appropriate names (i.e. the same name as these variable names) In one line?

Evan Pu
  • 2,099
  • 5
  • 21
  • 36
  • You might want to look at [this](https://stackoverflow.com/questions/3652851/what-is-the-best-way-to-do-automatic-attribute-assignment-in-python-and-is-it-a) – Luke Smith Nov 03 '17 at 19:32
  • 1
    Explicit is better than implicit... – juanpa.arrivillaga Nov 03 '17 at 19:33
  • "the same name as these variable names" ... what exactly do you mean here? – juanpa.arrivillaga Nov 03 '17 at 19:35
  • So, suppose you have some variables, `a, b, c`, and an a class, `Klass`, do you want `obj = Klass(a, b, c)` to result in an object with `obj.a, obj.b, obj.c` attributes? – juanpa.arrivillaga Nov 03 '17 at 19:38
  • @juanpa.arrivillaga Eurasia has always been at war with Eastasia. – hobbs Nov 03 '17 at 19:38
  • @hobbs [heretic!](https://i.imgur.com/SCIa8Im.jpg) – juanpa.arrivillaga Nov 03 '17 at 19:40
  • The entire purpose of using a programming language instead of writing raw machine code is to gain the advantage of making things implicit instead of explicit. – hobbs Nov 03 '17 at 19:42
  • This question is related to this: https://stackoverflow.com/questions/3652851/what-is-the-best-way-to-do-automatic-attribute-assignment-in-python-and-is-it-a – quamrana Nov 03 '17 at 19:49
  • @hobbs sure, I don't disagree. I would say that good programming languages provide a level of abstraction that make *certain things* implicit, e.g. the nitty-gritty details of messing with registers and machine code instructions. However, within a plane of abstraction, there are certain things that benefit from staying explicit. In this case, we have a function that takes 3 arguments and assigns them as attributes. Solutions that update attributes dynamically won't catch accidentally passing the wrong number of arguments, or the wrong names, for example. – juanpa.arrivillaga Nov 03 '17 at 19:55
  • @hobbs plus, *practicality beats purity* ;) – juanpa.arrivillaga Nov 03 '17 at 19:59
  • @juanpa.arrivillaga the ones I use catch those things just fine :) That's the kind of thing that a computer is not only *capable* of checking, but *better* at checking. And after writing enough "explicit" constructors you're eventually going to slip and write something like `self.reciever = receiver` and fail to notice it until it causes a bug, but the computer won't make that mistake. – hobbs Nov 03 '17 at 21:20

2 Answers2

2

You could achieve your goal with:

class My:
    def __init__(self, **kwargs):
        for k,v in kwargs.items():
            setattr(self, k, v)

my = My(foo=10, bar=12)

print(vars(my))

See here for Documentation.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
  • The only drawback here is that the names of the member variables are set by the client. It looks like the OP want to get the class itself to define the names. – quamrana Nov 04 '17 at 11:03
  • @quamrana That depends on your usecase. For sophisticated ways, one might look at http://www.attrs.org/en/stable/ – Thomas Junk Nov 04 '17 at 13:25
0

In Python 3.7+, the new dataclasses module seems like it would be a good fit for this. You have to annotate the type of the argument though, which may or may not be a plus for you.

Alok Singh
  • 55
  • 1
  • 5