Timeline for How do I avoid the "self.x = x; self.y = y; self.z = z" pattern in __init__?
Current License: CC BY-SA 4.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 27, 2018 at 9:44 | history | edited | Rakete1111 | CC BY-SA 4.0 |
deleted 11 characters in body
|
Feb 5, 2016 at 3:24 | comment | added | gruszczy | I added one more disclaimer that this is only to show capabilities of the language and it shouldn't be used in real projects | |
Feb 5, 2016 at 3:10 | history | edited | gruszczy | CC BY-SA 3.0 |
added 374 characters in body
|
Feb 5, 2016 at 0:41 | comment | added | djechlin | What's wrong with it is that it is extra lines of code without improving readability. If that's pythonic then okay, but it doesn't mean there's nothing wrong with it. | |
Feb 4, 2016 at 16:23 | comment | added | Joran Beasley | holy crap ... I should have put more effort into my answer ... no idea it would get 42 upvotes ... | |
Feb 4, 2016 at 12:10 | comment | added | Pedro |
@gerrit The only thing I can think of (I'm typing on my phone so I can't test) is that setattr() might trigger setter logic if you have a @property.setter in your object, whereas the dictionary update won't. Other than that, they are equivalent.
|
|
Feb 4, 2016 at 8:26 | comment | added | gerrit | @Pedro Is there any semantic difference between gruzczy's and JoranBeasley's syntax? | |
Feb 4, 2016 at 6:43 | comment | added | Gere | You are right, sorry. I had the suspicion that the user might not realize how bad it would be. | |
Feb 4, 2016 at 6:16 | comment | added | gruszczy | I also bolded the sentence that clearly states that the author of the question should do this explicitly. | |
Feb 4, 2016 at 6:16 | history | edited | gruszczy | CC BY-SA 3.0 |
added 4 characters in body
|
Feb 4, 2016 at 6:01 | comment | added | gruszczy | I believe the first two paragraph discourage from using the method. The user asked for how something is done and I think he/she received enough feedback that he/she shouldn't do that. It should still be useful for him/her to learn about this approach, even if he/she won't use it. | |
Feb 4, 2016 at 5:51 | comment | added | Gere | As others said, the suggestion is really poor programming style. It hides crucial information. You can show it, but you should explicitely discourage the OP from using it. | |
Feb 4, 2016 at 4:47 | comment | added | Pedro |
@JoranBeasley Updating the instance dictionary blindly with kwargs leaves you open to the equivalent of an SQL injection attack. If your object has a method named my_method and you pass an argument named my_method to the constructor, then update() the dictionary, you just overwrote the method.
|
|
Feb 4, 2016 at 3:09 | comment | added | MWB |
The problem with this approach is that there is no record of what arguments A.__init__ actually expects, and no error checking for mistyped argument names.
|
|
Feb 4, 2016 at 1:30 | history | edited | gruszczy | CC BY-SA 3.0 |
added 101 characters in body
|
Feb 4, 2016 at 1:30 | comment | added | Joran Beasley |
good answer +1 ... although self.__dict__.update(kwargs) might be marginally more pythonic
|
|
Feb 4, 2016 at 1:29 | history | answered | gruszczy | CC BY-SA 3.0 |