2

I have this:

class MyClass:
 """A simple example class"""
 i = 12345
 def f(self):
 print i # self.i will work just fine
 return 'hello world'

When I do:

>>> x = MyClass()
>>> 
>>> x.f()

I get an error, as expected.

My question is:

  1. Why do I get the error?

  2. Why is there no namespace between the namespace of the function(or method) definition and the global namespace of the module containing the class?

  3. Is there any other way to reference i inside f in this case other than using self?

asked Jan 12, 2013 at 23:23
8

2 Answers 2

3
  1. You've got an error because print i is trying to print a global (for the module) variable i. If you want to use the member of the class you should write self.i.
  2. Because Guido Van Rossum decided not to use namespaces. Actually, I don't know how to answer anymore.
  3. Yes, but no. Yes, because you can use "reflection" (I can't remember how it is called in python) and access any member of the class. No, because using self is the only usable way.
ford
12.1k4 gold badges50 silver badges58 bronze badges
answered Jan 12, 2013 at 23:26
Sign up to request clarification or add additional context in comments.

1 Comment

For 2: It doesn't make sense to make classes a namespace. What would it mean if you altered the contents of a mutable i? Is that changed for the class object, the instance, for subclasses? Explicit is better than implicit, so for classes you have to be explicit about what you want to reference. A class attribute, an inherited class attribute, or the instance attribute itself.
0

Why do I get the error?

Answer: This is because the variable i & the symbol/level f which points to a function/method code are in MyClass namespace where as the value/callable code/body of the function f lies in global namespace.

Why is there no namespace between the namespace of the function(or method) definition and the global namespace of the module containing the class?

Answer: Not sure why the python designer decided to put function name/symbol in the class but its body in global namespace.

Is there any other way to reference i inside f in this case other than using self?

Answer: I don't think so, but even if it is then it might not be easier logic/code

Nguyễn Văn Phong
14.2k19 gold badges48 silver badges65 bronze badges
answered Feb 2, 2020 at 3:16

1 Comment

about the last point: i is a class variable. Yes, you can reach it via self.i (i.e. python looks in the instance variables and if it cannot find it, it look into the variables of the class), and you can also reach it via MyClass.i

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.