0

I am learning OOP in python and following this tutorial. I am confuse about an example which is shown in this article :

def f(x):
    f.counter = getattr(f, "counter", 0) + 1 
    return "Monty Python"


for i in range(10):
    f(i)

print(f.counter)

I have some doubts.

I know what getattr(f, "counter", 0) do. Its give optional values of key to dict module. If an attribute name is not in included in either of the dictionary, the attribute name is not defined.

First i thought f.counter = getattr(f, "counter", 0) + 1 is doing same work as count do so i replaced f.counter = getattr(f, "counter", 0) + 1 like this:

def f(x):
    count=0
    count+=x
    print(count)

    return "Monty Python"


for i in range(10):
    f(i)


print(count)

its working fine but last line print(count) is giving error. So my confusion is why count is not global but when i used f.counter = getattr(f, "counter", 0) + 1 then f.counter was global how??

second question is :

Article says

"This can be used as a replacement for the static function variables of C and C++, which are not possible in Python."

so what is called static function??

2
  • 1
    count isn't defined in a global scope, only the scope of f. You can't access it outside of f. Side note: function attributes are almost never used, and I would question any tutorial that recommends them. Commented Oct 31, 2016 at 19:16
  • The article comment should be read as static function variables, that is, it is the variables that are static. In C and C++ there is a qualifier called 'static' which means they have a lifetime of the program unit. If you don't know C and C++ then its probably best to ignore comments that compare Python with those languages. Python is WAY more powerful. Personally I would use a closure in Python to achieve the effect. Commented Oct 31, 2016 at 19:20

2 Answers 2

1

That's because you were assigning a value to an attribute of f, which is global. Any changes you make to a global object's attribute is globally visible (even though in this case your global object is a function and it's not usual to work with attributes in function). When you made your changes, you created a count variable inside your function's scope, thus not being accessible outside of it.

Sign up to request clarification or add additional context in comments.

Comments

0

It's important to note that your function creates a variable count every time the function is run, and then once the function exits, count disappears. You could also write the function like this:

def f():
    f.counter = getattr(f, 'counter', 0) + 1

and f.counter would still hold the number of times f had been executed. We can access f.counter because we can access f, and nothing is private in Python. If we try to reference counter by itself outside the function, it will be a NameError, but only because it looks in every namespace it can find and doesn't see anything named counter. You would have to reference f.counter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.