PEP8 - instance attribute defined outside __init__

2,162 views
Skip to first unread message

Rudi Hammad

unread,
Oct 12, 2017, 1:34:44 PM10/12/17
to Python Programming for Autodesk Maya
Hello,

I usually turn off PEP 8 inspections in pycharm, but I gave it a try and turned inspections on to see what complains I get.
I didn´t get much so far, but one that made me think if I am programming right is   "instance attribute defined outside __init__"
Basically I am doing a UI in pyside, and as learned reading programming articles and also in this forum, I am trying to chop my code in as many methods as needed to keep things clean and easy to track.
So I have a method called create_mainLayout() where I organize the big layout blocks. I write something like this:

def create_mainLayout(self):
    self.L_vLao = QtGui.QVBoxLayout()
bla bla bla

And here is where I get the PEP 8 complain mentioned: "instance attribute defined outside __init__"

The reason why I do this is because then I have another method that picks up this self.L_vLao to get into more detail.
So if i want to use a variable created in another method, I have no choice but using self.
I thought creating self.something inside methods was okey.

Is my code badly designed?

Thank you,
R

Justin Israel

unread,
Oct 12, 2017, 2:26:17 PM10/12/17
to Python Programming for Autodesk Maya
It's a readability warning suggesting that it may make it harder for others to see where attributes are defined when they are initialized from other methods. It can also warn about potential bugs if you were to reorder methods in your init and accidentally put a method that uses the attribute before the one that defines it. 

You can satisfy PyCharm by initializing the attribute to None in your init. This should also allow your auto-complete to report the attribute for your class, if it doesn't already. Or you could disable the warning and choose not to make that part of your style. 


Thank you,
R

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/5e4ad9fa-ebfb-4868-ad92-1391d1efca29%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
Oct 12, 2017, 2:27:31 PM10/12/17
to Python Programming for Autodesk Maya


On Fri, Oct 13, 2017, 7:25 AM Justin Israel <justin...@gmail.com> wrote:


On Fri, Oct 13, 2017, 6:34 AM Rudi Hammad <rudih...@gmail.com> wrote:
Hello,

I usually turn off PEP 8 inspections in pycharm, but I gave it a try and turned inspections on to see what complains I get.
I didn´t get much so far, but one that made me think if I am programming right is   "instance attribute defined outside __init__"
Basically I am doing a UI in pyside, and as learned reading programming articles and also in this forum, I am trying to chop my code in as many methods as needed to keep things clean and easy to track.
So I have a method called create_mainLayout() where I organize the big layout blocks. I write something like this:

def create_mainLayout(self):
    self.L_vLao = QtGui.QVBoxLayout()
bla bla bla

And here is where I get the PEP 8 complain mentioned: "instance attribute defined outside __init__"

The reason why I do this is because then I have another method that picks up this self.L_vLao to get into more detail.
So if i want to use a variable created in another method, I have no choice but using self.
I thought creating self.something inside methods was okey.

Is my code badly designed?

It's a readability warning suggesting that it may make it harder for others to see where attributes are defined when they are initialized from other methods. It can also warn about potential bugs if you were to reorder methods in your init and accidentally put a method that uses the attribute before the one that defines it. 

You can satisfy PyCharm by initializing the attribute to None in your init. This should also allow your auto-complete to report the attribute for your class, if it doesn't already. Or you could disable the warning and choose not to make that part of your style. 

On another note, does Pep8 warn about your choice of method and variable naming? ;-) 

Rudi Hammad

unread,
Oct 12, 2017, 3:40:19 PM10/12/17
to Python Programming for Autodesk Maya


On another note, does Pep8 warn about your choice of method and variable naming? ;-)

oooh yes...pep8 complain about that as well...¬¬ I told him to not warn me about that.

PEP8 says no camelCase in variables. All lower case with underscoreY. L_vLao is ugly i know. stands for left vertical layout. I wrote capital "L" because in my rigs I do L_arm_jnt,
According to PEP8 it should be left_vertical_layout...come on! PEP8 variable naming convention would make variables name too long. How would you name it?
About the method name I tied to name it with according to the action that it does. I thought create_mainLayout was ok.

I try not be to obsessed with PEP8, but please, I am happy to hear how to name better variables and methods.

Justin Israel

unread,
Oct 12, 2017, 3:56:06 PM10/12/17
to Python Programming for Autodesk Maya
I'm not too fussed about camelCase vs snake_case but I do believe you should be consistent for readability. Also, upper case is a convention usually reserved for classes and globals. You have chosen to mix all of those in multiple combinations. It hurts readability. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Rudi Hammad

unread,
Oct 12, 2017, 5:25:10 PM10/12/17
to Python Programming for Autodesk Maya
True, L_vLao is a terrible choice.
thx


El jueves, 12 de octubre de 2017, 21:56:06 (UTC+2), Justin Israel escribió:


On Fri, Oct 13, 2017, 8:40 AM Rudi Hammad <rudih...@gmail.com> wrote:


On another note, does Pep8 warn about your choice of method and variable naming? ;-)

oooh yes...pep8 complain about that as well...¬¬ I told him to not warn me about that.

PEP8 says no camelCase in variables. All lower case with underscoreY. L_vLao is ugly i know. stands for left vertical layout. I wrote capital "L" because in my rigs I do L_arm_jnt,
According to PEP8 it should be left_vertical_layout...come on! PEP8 variable naming convention would make variables name too long. How would you name it?
About the method name I tied to name it with according to the action that it does. I thought create_mainLayout was ok.

I try not be to obsessed with PEP8, but please, I am happy to hear how to name better variables and methods.


I'm not too fussed about camelCase vs snake_case but I do believe you should be consistent for readability. Also, upper case is a convention usually reserved for classes and globals. You have chosen to mix all of those in multiple combinations. It hurts readability. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages