Domanda

EDIT: My mistake - there was another method name (with the underscore) at another place which had only a pass in its body. Stupid me. Please close.

I have a class with two methods, each returning a simple string. The only slightly qualitative difference between these two methods is that one has an underscore in its name while the other doesn't. However, the one with the underscore does not return the string when called. Using underscores in names of methods seems to follow PEP 8 guidelines regarding method names.

I have been reading the Python tutorial on classes thoroughly but it doesn't mention any difference in behaviour depending use of underscore inside the method name. If I remove the underscore, the string will be returned.

What's causing this?

Here's my code:

class Board:
    def f(self):
        return 'Hello'
    def print_board(self):
        return 'Hello'

I'm using Python 3.2.3.

È stato utile?

Soluzione

It works fine for me with Python 3.2.1 and 3.3.0:

class Board:
    def f(self):
        return 'Hello'
    def print_board(self):
        return 'Hello'


b = Board()
print(b.f())
print(b.print_board())

Executed at Windows, it prints:

c:\tmp\_Python>c:/python32/python a.py
Hello
Hello

c:\tmp\_Python>c:/python33/python a.py
Hello
Hello

Possibly you wanted to write:

...
    def print_board(self):
        print('Hello')

and call it

...
b.print_board()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top