Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Attributes
#1
I'm new to Python, and generally speaking am not trained as a programmer. I am using version 3.7 on Windows 8.1, and most of my work so far has been GUI related with wxPython (4.0.3). I have read various parts of PEP 8 in an attempt to learn to program "Pythonically" and so far, I think I'm doing a reasonable job.

My question has to do with the lack of private attributes, and whether it is necessary or "pythonic" to try to limit the numbers of publicly available attributes defined like: self.attribute = "string". To make various aspects of the class available to instance methods, I'm finding myself prepending almost every attribute with self. The result is that when writing code, intellisense presents everything from strings to sizers as available attributes from outside the class, even though in most cases they aren't needed outside the class.

Does this only appear to be an issue due to the use of intellisense (I'm using VS Code as an IDE), or is there a bigger issue around public attributes that I should concern myself with? I have read numerous posts and articles about how to create private-like attributes using single and double underscores, but I don't have a feeling yet regarding if or where I should apply this technique, and how liberally I should do so.

Thanks in advance for the feedback.
Reply
#2
It's not something you generally worry about in Python. Underscores can be used, but they are just a hint to other programmers that it's something internal to the class. If there are real security concerns, as in when self.x is changed self.y must also be changed or things crash, look into properties.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Nov-02-2018, 02:33 PM)ichabod801 Wrote: It's not something you generally worry about in Python. Underscores can be used, but they are just a hint to other programmers that it's something internal to the class. If there are real security concerns, as in when self.x is changed self.y must also be changed or things crash, look into properties.

Thank you for the response. I did read a fair bit about properties as well, but it wasn't immediately clear at the time how often and under what circumstances they should be used. Your mention of using it to manage attribute dependencies is a good one and makes a lot of sense.
Reply
#4
(Nov-02-2018, 02:15 PM)CanadaGuy Wrote: My question has to do with the lack of private attributes, and whether it is necessary or "pythonic" to try to limit the numbers of publicly available attributes
From the language's point of vue, you can have as many attributes as you want and we could say it is an intellisense issue or limitation. From the point of view of OOP design, I'd say that there is a design problem if a class has too many attributes. It means that some of these attributes could be grouped together and form subojects aggregated to the initial object.
Reply
#5
(Nov-02-2018, 02:57 PM)Gribouillis Wrote: From the point of view of OOP design, I'd say that there is a design problem if a class has too many attributes. It means that some of these attributes could be grouped together and form subojects aggregated to the initial object.

OOP is not one of my strong suits yet, and your response reminded me that I would like to address that as well. Is there a Python structure that is well suited to grouping attributes together? In my current code, I have a bunch of wx.StaticText, wx.TextCtrl, and sizers, among a few other widgets. I have yet to identify a design pattern (MVC, MVP, etc.) to follow which I realize is pretty important as the application grows larger. That said, can I group these widgets outside of a to-be-selected design pattern, or would the pattern itself achieve that in a more cohesive manner?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python the regex not getting any attributes sarath_unrelax 1 1,824 Dec-19-2019, 11:06 AM
Last Post: Larz60+
  Python QGIS tool that replaces layout text labels with attributes from an input table geodenn92 1 2,629 Aug-13-2019, 06:05 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020