xxxxxxxxxx
In Python, this:
my_object.method("foo")
is syntactic sugar, which the interpreter translates behind the scenes into:
MyClass.method(my_object, "foo")
which, as you can see, does indeed have two arguments - it's just that the first one is implicit, from the point of view of the caller.
This is because most methods do some work with the object they're called on, so there needs to be some way for that object to be referred to inside the method. By convention, this first argument is called self inside the method definition:
class MyNewClass:
def method(self, arg):
print(self)
print(arg)
If you call method("foo") on an instance of MyNewClass, it works as expected:
>>> my_new_object = MyNewClass()
>>> my_new_object.method("foo")
<__main__.MyNewClass object at 0x29045d0>
foo
Occasionally (but not often), you really don't care about the object that your method is bound to, and in that circumstance, you can decorate the method with the builtin staticmethod() function to say so:
class MyOtherClass:
@staticmethod
def method(arg):
print(arg)
in which case you don't need to add a self argument to the method definition, and it still works:
>>> my_other_object = MyOtherClass()
>>> my_other_object.method("foo")
foo
xxxxxxxxxx
This error is often caused by the fact that the self is omitted as a parameter in the method of the class.
https://careerkarma.com/blog/python-takes-one-positional-argument-but-two-were-given/#:~:text=Conclusion,the%20methods%20in%20a%20class.
0
xxxxxxxxxx
#Positional arguments are the amount of arguments passed through a function
#For example
def function(value1, value2):
print(value1)
print(value2)
function("value1","value2","value3")
xxxxxxxxxx
# Employee Class
class Employee:
# Get Employee method with self parameter
def GetEmployeeID(self,name):
print(f"The Employee ID of {name} ", 1234)
# instance of the employee
empObj = Employee()
empObj.GetEmployeeID("Chandler Bing")
xxxxxxxxxx
#if you're getting this issue out of a class function
#don't forget to add "self" as the first parameter for your function
class myClass:
def myFunction(self,argument1,argument2)
#calling
#self isn't defined when calling the function
test = myClass()
test.myFunction(1,2)
xxxxxxxxxx
[already Solved] TypeError: method() takes 1 positional argument but 2 were given
xxxxxxxxxx
import PyPDF2
import sys
filesList = sys.argv
# Create a new Object PdfFileWriter as blank PDF document
pdfWriter = PyPDF2.PdfFileWriter()
# Loop through all the all files specified on Arguments and then Iterate over all pages for each PDF file
for i in range(1, len(filesList)):
pdfFile = open(filesList[i], 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
for pageNum in range(pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
# Now that you have copied all the pages in both the documents, write them into the a new document
pdfOutput = open('bitslovers-merged.pdf', 'wb')
pdfWriter.write(pdfOutput)
# Close all the files
pdfOutput.close()
pdfFile.close()
xxxxxxxxxx
# just put self in other functions like this
class myclass:
def __init__(self, parameter):
self.parameter = parameter
def function(self, otherparameter):
# put a self ^ there
print(self.parameter + otherparameter)
object=myclass(1)
object.function(2)
# output is 3