This is the main class:
import Orange
def mainn():
orange = Orange()
print(orange.color)
The IDE is giving me this message 'Orange' is not callable.
And this the Orange class:
class Orange:
def __init__(self, color, weight):
self.color = color
self.weight = weight
def printorange(self):
print(self.weight)
print(self.color)
1 Answer 1
Orange is presumably your module name containing the class. You either need to do:
orange = Orange.Orange()
or you need to change the import to:
from Orange import Orange
If you're coming from a Java background, this may seem odd (since Java classes are the "module" there), but in Python, a module name and the class(es) (or lack thereof) defined within said module have nothing to do with one another; you could name the module Orange.py and define class Apple in it, and Python's fine with that, you just need to import it correctly and ask for the correct name within it.
Note that your code will still fail when you do this, because you neglected to provide the necessary arguments to the Orange initializer. You'll need to provide them (or define defaults for __init__'s arguments).
importis meant for packages/modules. What file isOrangein? If it’s inorange.py, you can do something likefrom orange import Orange.__init__method has no default parameters forcolorandweight. When you callOrange(), you need to provide values for them.importsucceeds, the file is definitelyOrange.py(which violates PEP8 naming rules, but beginners make this mistake all the time).