Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8676185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:07:51+00:00 2026-06-12T20:07:51+00:00

I have defined a class code.py class test: def __init__(self.x,self.y): self.x = x self.y

  • 0

I have defined a class

code.py

class test:
     def __init__(self.x,self.y):
       self.x = x
       self.y = y


    def sum(self):
      z = self.x + self.y
      return z

def main():
   x = test(1,2)
   valsum = x.sum()

I want the sum to be returned when the class is called not when the method id called?

How will I code it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:07:52+00:00Added an answer on June 12, 2026 at 8:07 pm

    Use a function for this:

    def test(*args):
        return sum(args)
    

    Or better, just use the builtin:

    sum((1,2))
    

    If you have good reason to want this, you can always wrap this stuff up in another function:

    def testsum(a,b):
        t = test(a,b)
        return t.sum()
    

    Or you can just do:

    result = test(a,b).sum()
    

    directly. This should be used sparingly though since it is inefficient …


    Finally (as pointed out in the comments, you can do this if you override __new__, DON’T DO THIS)

    class test(object):
        def __new__(cls,*args):
           return sum(*args)
    
    print (test(1,2,3,4)) #10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have this code class Test(object): def start_conn(self): pass def __init__(self): self.conn =
I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
import threading, time class test(threading.Thread): def __init__(self,name,delay): threading.Thread.__init__(self) self.name = name self.delay = delay
I tried this example code: class testclass: classvar = 'its classvariable LITERAL' def __init__(self,x,y):
class test: def __init__(self): test_dict = {'1': 'one', '2': 'two'} def test_function(self): print self.test_dict
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have the following structure in my code: #test.pyx cdef class BClass: cdef str
I have the following Python (3.2) code: from pygame import * class Application: def
I have a class defined in a gem by Diff::LCS and I want to
Here is a simple code illustrating the essence of a problem: class test: def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.