-6

I came to know we can also make class as callable without implementing the __call__() function, and I am trying to write some thing like that but I got stuck: below is my code

class neg:
    def __init__(self): # Classes are callables too
        self.val = 54545 
    def __repr__(self): 
        return str(self.val)

if __name__=='__main__':
       x=neg
       print(x) #print(neg()) also shows nothing
       #added by Merlin
       print('value of x:', x ) 

Executing the above does nothing, it doesn't print, What i am missing here?

The example i read to make class callable:

class Negate:
    def __init__(self, val): # Classes are callables too
    self.val = -val # But called for object, not work
    def __repr__(self): # Instance print format
    return str(self.val)
     actions = [square, sobject, pobject.method, Negate] # Call a class too
    for act in actions:
    print(act(5))

I fixed the main it does work, but when they say a class are callable then it shouldn't be neg() wasn't it?

5
  • The __init__ method is an initializer. That's what happens when you create an instance of that class. It's in no way a "callable" in the traditional sense. Please read the docs, or tell us specifically what you're trying to achieve. Commented Jun 18, 2016 at 18:40
  • "Executing the above does nothing, it doesn't print" You must be doing something wrong, that exact code sample printed out <class '__main__.neg'> when I ran it. Commented Jun 18, 2016 at 19:18
  • Tadhg, i corrected the mistake as per the suggestion, didn't you saw my highlighted text? Commented Jun 18, 2016 at 19:19
  • try calling : x=neg() Commented Jun 19, 2016 at 1:19
  • The question makes no sense as asked and conflates multiple concepts. The actual problem is caused by a typo (failing to create an instance of the class). The reason why the typo is a typo (why instance need to be created, and how they differ from the class itself) is explained by the linked duplicate. Nothing in the code shown has anything to do with making the class callable. Classes are callable by default; calling them creates an instance. The __call__ method (not shown here) can be used to make instances callable. Commented May 2, 2024 at 22:50

2 Answers 2

1

You need to create an instance of the class to run the __init__ method. And you also need to make sure your program starts up. You call/instantiate a function by writing do_something(); you call a class by writing neg().

if __name__ == '__main__': # Fix the main so program starts
    x = neg() # Call the class!
    print(x)

Seeing that you commented that you need to "call" the class, have you tried experimenting with it? You should've gotten the answer. Look at a shell:

>>> neg
<class __main__.neg at 0x103842db8>
>>> neg()
5435                    # Displays the __repr__ return value.

Whether you're writing a function or a class, you don't need to implement __call__ unless you're doing something complicated. Please, find some tutorials or read the docs.

Sign up to request clarification or add additional context in comments.

Comments

0

Classes are callable, only in the sense that you "call" a class to create an instance of the class. You call a class with the same syntax that you call functions, by using parenthesis and optional arguments. This is unrelated to call, which only affects instances.

7 Comments

@Explorer_N: I don't quite understand your comment. I don't know what you mean by "callable class".
Bryan, that is the first thing hits in my mind when i read the example(where they say classes are callable).. finally as everyone said they making object. then they shouldn't say classes are callable should they?
@Explorer_N: what example are you talking about? Who is "they"?
They Hmm Hopefully the person who brings the idea and written like classes are callable,the example I posted above.....
@Explorer_N: where did that example come from? Website? Book? Video? You are being very vague.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.