Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python :TypeError: this constructor takes no arguments

When the user enters an email address, and the program reads the email and display it according to its criteria (e.g [email protected]), like criteria:

  • username is yeo.myy
  • domain is edu.co

I know its something to do with the "@".

this is the code

class Email:
    def __int__(self,emailAddr):
        self.emailAddr = emailAddr


    def domain(self):
        index = 0
        for i in range(len(emailAddr)):
            if emailAddr[i] == "@":
                index = i
            return self.emailAddr[index+1:]

    def username(self):
        index = 0
        for i in range(len(emailAddr)):
            if emailAddr[i] == "@" :
                index = i
            return self.emailAddr[:index]

def main():

    emailAddr = raw_input("Enter your email>>")

    user = Email(emailAddr)

    print "Username = ", user.username()
    print "Domain = ", user.domain()

main()

this is the error I got:

Traceback (most recent call last):
  File "C:/Users/Owner/Desktop/sdsd", line 29, in <module>
    main()
  File "C:/Users/Owner/Desktop/sdsd", line 24, in main
    user = Email(emailAddr)
TypeError: this constructor takes no arguments
like image 304
smuggyiz Avatar asked Feb 28 '26 10:02

smuggyiz


2 Answers

def __int__(self,emailAddr):

Did you mean __init__?

def __init__(self,emailAddr):

You're also missing a couple selfs in your methods, and your returns are improperly indented.

def domain(self):
    index = 0
    for i in range(len(self.emailAddr)):
        if self.emailAddr[i] == "@":
            index = i
            return self.emailAddr[index+1:]

def username(self):
    index = 0
    for i in range(len(self.emailAddr)):
        if self.emailAddr[i] == "@" :
            index = i
            return self.emailAddr[:index]

Result:

Username =  yeo.myy
Domain =  edu.co

Incidentally, I recommend partition and rpartition for splitting a string into two pieces on a given separator. Sure beats keeping track of indices manually.

def domain(self):
    return self.emailAddr.rpartition("@")[2]
def username(self):
    return self.emailAddr.rpartition("@")[0]
like image 134
Kevin Avatar answered Mar 02 '26 23:03

Kevin


This error may happen if you type def _init_ with a single underline instead of def __init__ with double underlines before and after init.

like image 28
Avelino Sanchez Avatar answered Mar 03 '26 00:03

Avelino Sanchez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!