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

  • SEARCH
  • Home
  • 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 6748719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:56+00:00 2026-05-26T12:35:56+00:00

i have done this in c++ but i am trying it to python and

  • 0

i have done this in c++ but i am trying it to python and have difficulties.I did this code:

class mycomplex:
def __init__(self,real=None,imag=None):
    self.real=real
    self.imag=imag

def read_data(self):
    self.real=input("Give the real part :")
    self.imag=input("Give the imag part :")

def addition(self,complex):
    return mycomplex(self.real+complex.real,self.imag+complex.imag)


def __str__(self):
    return ("{0} {1} {2} {3}{4}".format("The complex is : ",self.real,"+",self.imag,"j"))


if __name__=="__main__":

a=mycomplex()
b=mycomplex()
a.read_data()
b.read_data()
print(a)
print(b)
c=a.addition(b)
print(c)

1) First of all it doesn’t work because i have in the init method 2 arguments and when i try to create an instance with a=mycomplex() it gives me an error of course.Can i handle this in some way without changing the init ?

2) In order for me to understand i want to use the addition method with 2 ways like i say in the code.It will help me i think.It’s different to say a.addition() and c=a.addition(b).

3) If you have any better suggestions from implementing the above,please say it.I think you understand what i am trying to show you.

Thank you!

  • 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-05-26T12:35:57+00:00Added an answer on May 26, 2026 at 12:35 pm

    1) yes you can make it work by using default values:

    def __init__(self,real=0,imag=0):
        ...
    

    the default values specified in the function signature will be used in place of the missing arguments.

    2) i do not understand your first way of writing an addition: when you write a.addition(), what are you adding to a ?

    the second way (a.addition(b)) is pretty clear, but you need to change the way you define addition():

    def addition(self,complex):
        return mycomplex(real+complex.real,imag+complex.imag)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume we have a class in python: class A(object): def __del__(self): print Del!
Okay, I have done a bit of searching online and found this thread, but
I've done quite a bit of googling on this error but have had no
I know this question has been done but I have a slightly different twist
I don't think this can be done cleanly, but I'll ask anyway. I have
I have never done Python programming before but I have done a lot of
I have done this: $ z() { echo 'hello world'; } How do I
I have done this operation millions of times, just using the + operator! I
I could have sworn i have done this somewhere - im using 2.0 right
I need to create a Firebird Database programmatically using DBExpress. I have done this

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.