how to create an instance of class python

jani

I am learning about classes in Python, using Python 3.4.1 and am attempting to make a class and then call the methods from that class. I have looked at other questions related to this and unfortunately, I can't seem to make it work. Here is my class (copied straight from the book)

class Counter(object):
    """Models a counter"""
    instances = 0
    def __init__(self):
        """Sets up counter"""
        Counter.instances += 1
        self.reset()
    def reset(self):
        """Sets counter to 0"""
        self._value=0
    def increment(self, amount = 1):
        """Adds amount to counter"""
        self._value += amount
    def decrement(self, amount = 1):
        """Subtracts amount from counter"""
        self._value -= amount
    def getValue(self):
        return self._value
    def __str__(self):
        return str(self._value)
    def __eq__(self, other):
        """Returns True if self == other and False if not"""
        if self is other:
            return True
        if type(self)!=type(other):
            return False
        return self._value==other._value

and this is how I'm calling it from another file (in the same folder):

import Counter
h = Counter()
print(h.getValue())

and this is the error I'm getting:

Traceback (most recent call last):
File "C:/Python34/learning/classtest.py", line 3, in <module>
h = Counter()
TypeError: 'module' object is not callable

I can type import Counter just fine into the shell, but when I get to h = Counter() I get the same error. I know I'm doing something wrong, but what?

Martijn Pieters

You named your module Counter too; the class is contained in the module:

import Counter

h = Counter.Counter()

Alternatively, import the class from the module:

from Counter import Counter

h = Counter()

This is why the Python style guide recommends you use all lower-case names for your modules. In Python, the module name does not have to match the class contained, and you are not limited to just one class in a module. A module can contain just functions or any other Python object too.

Had you named your module file counter (all lowercase) instead, it would perhaps have been more obvious that the module and contained class are two distinct concepts. :-)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to copy a class instance in python?

how to dynamically create an instance of a class in python?

How to fix 'Cannot create an instance of ViewModel class'?

How to create an instance of a class from a String in Swift

How to create a random instance of a case class?

Is it safe to create an instance of a class in its metaclass in Python?

How to create instance of a class and inject services?

How can I create a class instance from a list Python

Create an instance in a class method in Python

How to create an instance of a subclass from the super class?

How to initialize a class in python, not an instance

How to create and delete class instance in python for loop

Python create instance of class using string manipulation

How to create an instance by using the name of the class of another instance in python?

Create a vim instance from a python class?

How to create an instance of the class is created at run time

create an instance of class in python 2.7

How to persist instance of a class in Python

Python: how to get create instance of a class within same class?

How to create an instance of the EntityDescriptor class

How to create an instance of a class inside a method within the class, python

How to create class instance using class as variable?

How to create an optional instance of a case class?

How to create instance of nested class?

How can I create an instance of class dynamically in python

How to create a class that is not an instance of type?

How can i create a instance and add the instance to another class in python

How do I create an instance of a nested class from within that class in Python?

How to create the attribute of a class object instance on multiprocessing in python?