Python Forum
understanding lists....I'm very confused
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
understanding lists....I'm very confused
#8
class foo():
   def __init__(self, bar):
      self.moo = bar #technically this 'runs' 
      #and visually someone starting out may misconstrue that attribute=parameter (bar in this case)
      #but self.moo is really the attribute.  WHY AM I ALLOWED to name self.moo as anything I want? 
      #what is the rationale here?  It really should be self.bar = bar...and this is convention, but         
      #nothing really stops us from changing attr to self.moo 
It is extremely common in ALL languages to use attribute names that are different from the parameters that set them. Other OOP languages support "data hiding" where attributes/fields are not directly accessible. Python does not have this. In other words, using your example I could do this:

class Foo:
    def __init__(self, bar):
        self.moo = bar


f = Foo()
f.moo = "MOOOOOOOOO"
So in Python we follow a convention where we put an underscore '_' in front of fields that shouldn't be touched:

class Foo:
    def __init__(self, bar):
        self._moo = bar
So why does the language let you? Because all you're doing is assigning the value of one variable (bar) to another (self._moo). The language itself doesn't (and can't) attribute semantic meaning to variable names. It's up to you, the programmer to write clear, clean, and understandable code. And I will let you in on a little secret: lots of programmers write muddled, ugly, unintelligible code. But it works so they think they're a success.

Quote: I want to ask why do you run directly through bash? what is the advantage? I ask bc I started out coding directly through the terminal (linux) -but didn't like it. Moved to idle, and now go through geany (atom feels buiky). Any advantage to running thru bash(or terminal) and python3 vs geany (& other ides)f5???

I do the bulk of my development work with Vim and bash simply because I'm very comfortable with those tools. I use and like PyCharm, and have used Eclipse and Visual Studio for Java and C/C++/C# development as well. In this particular case, to copy and paste a little code and run it quickly, there was no reason to fire-up an IDE.
Reply


Messages In This Thread
RE: understanding lists....I'm very confused - by mpd - Dec-23-2017, 02:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,084 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 972 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,570 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,720 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,924 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,197 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,381 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  I am really confused with this error. Runar 3 3,029 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,499 Feb-10-2020, 09:15 AM
Last Post: snippsat
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,288 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

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