Problem Running Module in PyCharm 96.742 Console

已回答

I created a new class in Python:

__author__ = 'Michael'

class chapter2(object):

    """Core Python Programming chapter2"""

    version = 1.0 # class static attribute

    def __init__(self, name='first class'):

        """constructor"""

        self.name = name

        print 'Create your first Python class'

    def show(self):

        """display instance attribute and class name"""

        print ' instance ', self.name

        print ' class ', self.__class__.__name__

        print ' version ', self.version

    def exercise1(self):

        """Core Python Programming exercise 2.1"""

        s = 'Duffy'

        i = 2

        x = 3.141592643489759

        print 's = ', s

        print 'i = ', i

        print 'x = ', x

    def exercise2(self):

        """Core Python Programming exercise 2.2"""

        return 1 + 2 * 4

I can import the class successfully, but when I use "dir()" I don't see function "exercise2", and I can't instantiate an instance.  Here's the output I see in the console:

C:\Tools\Python-2.6.1\python.exe -u C:\Program Files\JetBrains\PyCharm 96.742\helpers\pydev\console\pydevconsole.py 2896 2897

import sys; print('Python %s on %s' % (sys.version, sys.platform))

Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32

sys.path.append('C:/Documents and Settings/Michael/My Documents/Projects/Python/learning/core')

>>> import chapter2

>>> dir(chapter2)

['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'chapter2']

>>> ch2 = chapter2()

  File "<console>", line 1, in <module>

TypeError: 'module' object is not callable

Traceback (most recent call last):

Can anybody tell me what I'm missing?  Why can I not instantiate my class?  Why don't the methods exercise1() and exercise2() appear?  I've attached a screen shot.  Thanks.



Attachment(s):
pycharm-console-error.PNG
0
Avatar
Permanently deleted user

This error statement TypeError: 'module' object is not callable is raised as you are being confused about the Class name and Module name. The problem is in the import line . You are importing a module, not a class. This happend because the module name and class name have the same name .

If you have a class "MyClass" in a file called "MyClass.py" , then you should import :

from MyClass import MyClass

In Python , a script is a module, whose name is determined by the filename . So when you start out your file MyClass.py with import MyClass you are creating a loop in the module structure.

0

请先登录再写评论。