Python Forum
missing 1 required positional argument: 'self'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
missing 1 required positional argument: 'self'
#1
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
Reply
#2
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
Reply
#3
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".
Reply
#4
Hum, but both

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

Quote:TypeError: unhashable type: 'list'
Reply
#5
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.
Reply
#6
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()
Reply
#7
(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
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,817 May-09-2022, 12:49 PM
Last Post: deanhystad
  What is positional argument self? Frankduc 22 5,712 Mar-06-2022, 01:18 AM
Last Post: Frankduc
  TypeError: missing a required argument: 'y' gible 0 2,916 Dec-15-2021, 02:21 AM
Last Post: gible
  positional argument: 'self' mcmxl22 8 3,293 Dec-13-2021, 10:11 PM
Last Post: deanhystad
  TypeError: missing 3 required positional arguments: wardancer84 9 10,883 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 1,969 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 4,213 May-15-2021, 04:15 PM
Last Post: Anldra12
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 5,468 Jan-06-2021, 04:25 PM
Last Post: Insen
  Missing 1 required position argument: 'failure' while starting thread. BradLivingstone 3 4,112 Jun-19-2020, 02:31 PM
Last Post: stullis
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 12,290 Jun-17-2020, 07:25 PM
Last Post: sveto4ka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020