Python Forum
missing 1 required positional argument: 'self' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: missing 1 required positional argument: 'self' (/thread-27414.html)



missing 1 required positional argument: 'self' - yasser - Jun-06-2020

Hello

I am trying to create a class and recall the fucntion from the class but it give me error

phonenote={}
class phone:
    NumOfPersons=0

    def __init__(self, firstname, lastname, mobile ,worknumber,homenumber ):
        self.fistname=firstname
        self.lastname=lastname
        self.mobile=mobile
        self.worknumber=worknumber
        self.homenumber=homenumber
    def __str__(self):
        return ("First Name:" +" " +self.fistname + " \n"+"Last Name:"+ " "+self.lastname + "\n" + "Mobile No:"+ " " + self.mobile +"\n" "Work Number:" + self.worknumber + "\n" + "Home Number:"+ self.homenumber)
    def dicphone(self):
        phonenote={self.fistname + " " +self.lastname ,[self.mobile ,self.worknumber,self.homenumber]}
        print(phonenote)

ahmed=phone("ahmed" , "kamel" , "0103379489"  , "445543333" , "78777777" )

phone.dicphone()
error:

Error:
D:\Users\xx\PycharmProjects\untitled2\venv\Scripts\python.exe D:/Users/xx/PycharmProjects/untitled2/Class/Tutorials_1/Note_Phone.py Traceback (most recent call last): File "D:/Users/xx/PycharmProjects/untitled2/Class/Tutorials_1/Note_Phone.py", line 19, in <module> phone.dicphone() TypeError: dicphone() missing 1 required positional argument: 'self' Process finished with exit code 1



RE: missing 1 required positional argument: 'self' - roughstroke - Jun-06-2020

phone.dicphone()   #this is wrong 
ahmed.dicphone()   #this is right
and btw do not use dict{}and list[] at the same time they are both unhashable


RE: missing 1 required positional argument: 'self' - deanhystad - Jun-06-2020

You can use phone.dicphone(ahmed) which is what ahmed.dicphone() becomes after Python converts the text to bytecodes. Calling phone.dicphone() is missing a value for "self".


RE: missing 1 required positional argument: 'self' - ebolisa - Jun-06-2020

Hum, but both

ahmed.dicphone()
phone.dicphone(ahmed)
yeld error on line 14...

Quote:TypeError: unhashable type: 'list'



RE: missing 1 required positional argument: 'self' - deanhystad - Jun-06-2020

phonenote={self.fistname + " " +self.lastname ,[self.mobile ,self.worknumber,self.homenumber]}
evaluates to:
phonenote ={"ahmed kamel" , ["0103379489"  , "445543333" , "78777777"]} 
Which is not valid syntax for making a dictionary. Since there is no ":" I think python is trying to hash the name and the list of phone numbers.


RE: missing 1 required positional argument: 'self' - yasser - Jun-06-2020

Hello

I check your reply and now i need to append my dictionary : phonenote after that i will save it to json file.

my question how can i append my dictionary from inputs coming from :idphone?

phonenote={}
class phone:
    NumOfPersons=0

    def __init__(self, firstname, lastname, mobile ,worknumber,homenumber ):
        self.fistname=firstname
        self.lastname=lastname
        self.mobile=mobile
        self.worknumber=worknumber
        self.homenumber=homenumber


    def __str__(self):
        return ("First Name:" +" " +self.fistname + " \n"+"Last Name:"+ " "+self.lastname + "\n" + "Mobile No:"+ " " + self.mobile +"\n" "Work Number:" + self.worknumber + "\n" + "Home Number:"+ self.homenumber)
    def dicphone(self):
        print(idphone)
       

firstname=input("Please enter your first name")
lastname=input("Please enter your last name")
mobile=input("Please enter your mobile number")
worknumber=input("Please enter your work number")
homenumber=input("Please enter your work number")

idphone=phone(firstname,lastname,mobile,worknumber,homenumber)
idphone.dicphone()



RE: missing 1 required positional argument: 'self' - snippsat - Jun-06-2020

(Jun-06-2020, 08:35 PM)yasser Wrote: my question how can i append my dictionary from inputs coming from :idphone?
Here a smaller version with some fixes and a shortcut to making the dictionary Think
class Phone:
    def __init__(self, firstname, mobile):
        self.firstname = firstname
        self.mobile = mobile

    def __str__(self):
        return f"First Name: {self.firstname}\nMobile No: {self.mobile}"
Usage test:
>>> id_phone = Phone('Kent', 1122334455)
>>> id_phone.mobile
1122334455
>>> 
>>> print(id_phone)
First Name: Kent
Mobile No: 1122334455
>>> 
>>> phonenote = id_phone.__dict__
>>> phonenote
{'firstname': 'Kent', 'mobile': 1122334455}
>>> phonenote['mobile']
1122334455



RE: missing 1 required positional argument: 'self' - ndc85430 - Jun-07-2020

(Jun-06-2020, 04:27 PM)deanhystad Wrote:
phonenote ={"ahmed kamel" , ["0103379489"  , "445543333" , "78777777"]} 
Which is not valid syntax for making a dictionary. Since there is no ":" I think python is trying to hash the name and the list of phone numbers.

Yes, because that's how you write a set literal.