Python Forum
newb selfie - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: newb selfie (/thread-16210.html)



newb selfie - PatM - Feb-18-2019

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?


RE: newb selfie - micseydel - Feb-18-2019

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.


RE: newb selfie - Larz60+ - Feb-18-2019

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



RE: newb selfie - PatM - Feb-18-2019

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?


RE: newb selfie - ichabod801 - Feb-18-2019

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.


RE: newb selfie - snippsat - Feb-19-2019

(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