0

I am trying to play with classes in python. I tried to run the following code.

class Abc:
    def a(self):
        print ("not to be seen")
    def b(self):
        print("inaccessible is")
        self.a

say = Abc()
say.b

I am expecting the output as

inaccessible is
not to be seen

Instead I get the following output:

SyntaxError: invalid syntax

with say highlighted.

Please somebody point out what I am doing wrong.

Edit: I'm using IDLE GUI. Python 33 says the Python docs.

6
  • 5
    Please post the full exception message. Being able to read these is an important skill. Commented Jul 30, 2013 at 8:27
  • Please post the actual code (with indentation). Commented Jul 30, 2013 at 8:27
  • 4
    Apart from whatever results in the SyntaxError here, self.a is not actually calling anything, instead this syntax returns a function, that when called evaluates Abc.a(self). You want both self.b() and self.a() in your code. Commented Jul 30, 2013 at 8:29
  • Which version of Python are you running? Commented Jul 30, 2013 at 8:30
  • If you don't post the full exeption message including the traceback, we won't be able to help you. Commented Jul 30, 2013 at 8:37

3 Answers 3

2

Python likes to make syntax very clear - the ()s after a function are not optional when calling a function without parameters like in some other languages.

You're not calling the functions just 'stating' them.

Try

class Abc:
    def a(self):
        print ("not to be seen")
    def b(self):
        print("inaccessible is")
        self.a()

say = Abc()
say.b()

Here is the code working.

Syntactically, the code is valid.

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

2 Comments

"Python likes to make syntax very clear - the ()s after a function are not optional when calling a function without parameters like in some other languages.". It's semi correct, it wont call it but it won't produce a SyntaxError either
@limelights Right, but it wouldn't produce OP's expected result which is calling the function - just like in languages like JavaScript and C# and unlike other languages like CoffeeScript and Ruby.
2

You almost had it. You need to call the functions by adding (), like so:

class Abc:
    def a(self):
        print ("not to be seen")
    def b(self):
        print("inaccessible is")
        self.a()

say = Abc()
say.b()

Actually I'm puzzled why your code throws a syntax error. In Python, it is valid to state a function.

1 Comment

Still, tt says syntax error and the first say is highlighted. Isnt that how class objects are created? I'm using IDLE GUI. Its Python version 3.3.0 and relese 3.3
0

OK, I could reproduce your error by installing idle for Python 3.3.0. I'm sorry that we all suspected that you didn't include the whole error message because IDLE doesn't produce more than a red SyntaxError: invalid syntax. There is nothing wrong with your code, nor your class definition.

I guess, you're just pasting the code as-is into your Python shell window. This way, things won't work because the indentation doesn't get produced correctly. Try pasting the line class Abc: into your shell window and press Enter. You will see that IDLE automatically indents the next line with a tab. This is the correct indentation for the following line, so when you enter it, you need to paste def a(self): without any extra indentation! If you paste line by line and reduce the indentation by one where needed and terminate your class definition with an extra Enter, your code gets executed correctly.

However, you should better use the following method:

  • Paste your file into an editor and save it as whatever.py
  • In IDLE, choose File -> Open and open this file
  • A new window opens with your source code inside.
  • Now, press F5 or say Run -> Run Module
  • Your code will be executed and the result displayed in the Python Shell.

Or, even better, use Python directly in the shell by executing python whatever.py directly.

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.