0

In Python, if I have a class like

class Rectangle:
   x:int = 0
   y:int = 0
   width:int = 0
   height:int = 0

Is there a way to create an instance of it and setting all attributes all at once without a constructor?

Something like (obviously not working) :

rect = Reactangle() { x = 0, y = 0, width = 20, height = 30 }

I am aware of dictionaries, so I'm open to suggestions.

Edit

I am aware that I could do

class Rectangle:
  def __init__(x:int = 0, y:int = 0, width:int = 0, height:int = 0):
    self.x = x
    self.y = y
    self.width = width
    self.height = height

But that's not really a nice looking solution, with all the repetitions, when the class contains a dozen attributes, etc.

As read in the comments, it looks like dataclasses is the way to go.

deceze
  • 471,072
  • 76
  • 664
  • 811
Yanick Rochon
  • 45,203
  • 21
  • 112
  • 182
  • 2
    Reading between the lines, you may be looking for dataclasses: https://docs.python.org/3/library/dataclasses.html – deceze Nov 05 '20 at 16:10
  • 1
    That last line is essentially a call to a constructor. Why avoid a constructor call? – Carcigenicate Nov 05 '20 at 16:12
  • 1
    Also note that how you have your class defined, those are class attributes, not instance attributes. – Carcigenicate Nov 05 '20 at 16:13
  • Does this answer your question? [What are data classes and how are they different from common classes?](https://stackoverflow.com/questions/47955263/what-are-data-classes-and-how-are-they-different-from-common-classes) – mkrieger1 Nov 05 '20 at 16:15
  • I don't understand. You want to initialize an object but not use `__init__`? – anon01 Nov 05 '20 at 16:15
  • Or https://stackoverflow.com/questions/8682848/assign-function-arguments-to-self – mkrieger1 Nov 05 '20 at 16:16
  • 1
    Do you mean you don't want to call a constructor, or you don't want to need to manually write a constructor? – Carcigenicate Nov 05 '20 at 16:18
  • @Carcigenicate I was trying to avoid "modifying the attribute of an instance while instanciating a class". That's silly formulation, don't be pedantic :) – Yanick Rochon Nov 05 '20 at 17:01

1 Answers1

1

I suspect a class that uses __init__ would do what you want (it's a little unclear). Note that you've defined a class with class variables only. Something like this is more typical:

class Rectangle:
   def __init__(self, x:int=0, y:int=0, width:int=0, height:int=0):
       self.x = x
       self.y = y
       self.width = width
       self.height = height


# instantiation:
rect = Rectangle() # uses defaults

rect = Rectangle(x=1,y=2,width=3,height=4) # create instance and set attributes as you say

This is pretty vanilla. Alternatives include the attrs library or a dataclass

anon01
  • 8,116
  • 7
  • 25
  • 54
  • Yes, I'm aware about that, but I provided a simple example. My actual class has many attributes and it's silly to duplicate all of them in the constructor. I would prefer declaring the class attribute and then overriding them directly without arguments. (I have updated the question to explain this) – Yanick Rochon Nov 05 '20 at 16:54