1

I am a beginner in Python. I know spaces play a significant role for compiling. So I have the following two classes in the same python script file:

class ClassA:
  def __init__(self):
     self.x = {}
  def _methodA1():
    # Something
  def _methodA2():
    # Something

class ClassB:
  def _methodB1():
    # Something
  def _methodB2():
    # Something

def MethodX():
  print "Hello World"

(1) Which class MethodX belongs to?

(2) Can we say ClassB is an inner class of ClassA ?

(3) If MethodX belongs to ClassA, does it mean self.x is accessible via MethodX ?

7
  • MethodX doesn't belong to a class since it is not indented under a class. Commented Mar 30, 2016 at 18:03
  • 1
    It seems that something may be wrong with your formatting? As formatting is what your question is about I recommend you change that to get a more accurate answer. Commented Mar 30, 2016 at 18:04
  • 3
    Smells like homework Commented Mar 30, 2016 at 18:04
  • 1
    In Python, a line falls into the scope of the first less-indented line above it. Commented Mar 30, 2016 at 18:08
  • 1
    are you already a programmer? if so, the general idea with Python is that most programmers generally already indent code for readability in most languages. Python makes it formally significant. a big rule to keep in mind is that your indentation has to be consistent in a block (class, method, if, for, etc...) . i.e. you can 2 spaces indent (4 is norm) on methodA and 4 on methodB. but you cant mix and match multiple indents in the same block. Commented Mar 30, 2016 at 18:09

4 Answers 4

8
  1. MethodX belongs to no class, it's a global function, not a method of one of the classes
  2. No, because ClassA and ClassB are at identical indentation; an inner class of ClassA would match the indentation of the method definitions in ClassA
  3. Since it belongs to no class, MethodX has no direct access to attributes of any instance. Beyond that, self must be explicitly accepted as the first positional argument by methods; since MethodX doesn't accept self, it can't access it. All of your methods are broken because of this; unless decorated with @staticmethod, they need to accept at least one positional argument (which is called self by convention on regular methods, and cls by convention on @classmethods).
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your explanation
2

Yes, space is important to python, the level of indentation used for blocks and statements is how the scope of those statements is established.

MethodX isn't a method it's a top level function, not associated with an instance of an object or called via a Class.

ClassB isn't an inner class of ClassA. You can nest classes in python, because class introduces another level of block scope, which do nest, but it's quite unusual to do so, it doesn't confer many benefits, and it makes some scoping behaviours look more confusing (i.e. referencing class attributes in the enclosing class scope)

MethodX doesn't have access to ClassA.x via self because it's not in the scope of ClassA. Furthermore, you have to declare self as an explicit parameter in python - the reference to the object instance is passed automatically as the first parameter to methods, but there is no binding automatically created for it. In fact self is a convention, not a formal keyword. _methodA1 and _methodA2 do have method scope, because they're defined within the scope of ClassA, but they don't declare a self binding

Comments

1
  1. MethodX doesn't belong to either class A or B.

  2. ClassB is not an "inner" class of ClassB.

  3. Irrelevant because it's not the case.

2 Comments

Thank you for your explanation
Pleasure. It's short, but to the point. @ShadowRanger 's answer gives you a bit more of the why and what for, so I'd tick his (or hers) answer if you were to tick any.
0

Stuff with the same indentation is generally on the same "level" (unindented = "file level").

Block which is indented "belongs" to first statement before it, which is indented less than it.

Example:

class A:         # "file level"
    def b(self): # "belongs to" class A
        if True: # "belongs to" b
            pass # "belongs to" the if

def x(): # "file level"
    pass # "belongs to" x
    pass # same as above

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.