-1

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)
Nishant
22.2k20 gold badges80 silver badges111 bronze badges
asked Sep 15, 2020 at 13:16
4
  • 4
    import is meant for packages/modules. What file is Orange in? If it’s in orange.py, you can do something like from orange import Orange. Commented Sep 15, 2020 at 13:17
  • Your __init__ method has no default parameters for color and weight. When you call Orange(), you need to provide values for them. Commented Sep 15, 2020 at 13:19
  • 3
    @bfontaine: Given that the import succeeds, the file is definitely Orange.py (which violates PEP8 naming rules, but beginners make this mistake all the time). Commented Sep 15, 2020 at 13:20
  • Does this answer your question? Python module' object is not callable Commented Sep 15, 2020 at 14:43

1 Answer 1

3

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).

answered Sep 15, 2020 at 13:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.