Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newb selfie
#1
I'm trying to get a grasp on Python classes and want to clarify something.

If I use a variable name like "count" inside a class and also have a "count" variable outside the class then I *must* use self.count inside the class or python will use the global instead?

So when writing any class all "local" variables really have to be written as self.var or they could clash with variables in the main program?
Reply
#2
I'm pretty sure the short answer is yes (code examples go a long way with clarifying a question). The long answer is that that should probably not be happening, because you probably shouldn't have global variables. We can probably help with resolving that, if you're interested.
Reply
#3
without globals:
class ClassWithCount:
    def __init__(self):
        self.count = 0
        # dumb loop that does nothint useful
        while self.count < 100:
            self.count += 1
        print(self.count)


def Myfunc():
    count = 14

    print(f'local count: {count}')

    cwc = ClassWithCount()
    print(f'local count: {count}, cwc.count: {cwc.count}')


if __name__ == '__main__':
    Myfunc()
Output:
Output:
local count: 14 100 local count: 14, cwc.count: 100
Reply
#4
I thought there was no way to avoid global variables because of Python scoping? If I write a class for others to use I have no control over how they use variable so from what I can tell I'm stuck writing self in front of every variable. Seems to go completely against the rest of the python philosophy on less typing to get things done.

Is it because Python was originally a procedural language that has had OOP duct-taped on?
Reply
#5
Global variables are easy to avoid.

(Feb-18-2019, 08:51 PM)PatM Wrote: If I write a class for others to use I have no control over how they use variable so from what I can tell I'm stuck writing self in front of every variable.

This doesn't make any sense. You seem to be talking about Java style security for classes. Python does not have that, but that has nothing to do with self. You need to use self to clarify that you are accessing an attribute of the instance.

Python is and always has been a multi-paradigm language with OOP included. I don't know where you got the idea that OOP was duct-taped on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Feb-18-2019, 08:51 PM)PatM Wrote: If I write a class for others to use I have no control over how they use variable so from what I can tell I'm stuck writing self in front of every variable. Seems to go completely against the rest of the python philosophy on less typing to get things done.
Has to use something to difference it from functions as all langues dos, it will work with s or this,but self is what we all know.
There is a lot less typing in Python OOP even with self Shifty if eg compare to Java/C++,i mean it's not couple of lines bot a lot more.

To give a example with some modern features like super() f-string and a little older @property,which all make it nicer.
self is only used when needed.
class Foo:
    # No need for self on class attribute
    var = 'hello'
    count = 0
    def __init__(self):
        self.age = 42
        Foo.count += 1

class Bar(Foo):
    def __init__(self, name):
        super().__init__()
        self.name = name

    @property
    def future(self):
        # No need to us self as "year" only used in method
        year = 10
        print(f'In {year} year i am {self.age + year}')
Use:
>>> obj = Bar('Kent')
>>> print(f'{obj.var} my name is {obj.name} age is {obj.age}')
hello my name is Kent age is 42
>>> obj.count
1
>>> obj.future
In 10 year i am 52

>>> obj = Bar('Tom')
>>> obj.count
2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError issue with daughter's newb code MrGonk 2 1,406 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Simple newb string question Involute 2 2,169 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 5,845 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 2,388 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 19,143 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 5,741 Feb-28-2019, 06:04 PM
Last Post: buran
  Complete NEWB and openpyxl project Netopia 44 16,872 Jan-18-2019, 08:15 PM
Last Post: Netopia
  Newb Question - Threading in Crons vvarrior 2 2,718 Jul-20-2018, 08:12 PM
Last Post: vvarrior
  Matt's newb question 1 MattSS102 1 2,669 Aug-28-2017, 03:27 AM
Last Post: BerlingSwe
  Newb: Simple Explicit Formula Duplicitous 1 3,107 May-05-2017, 07:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

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