TypeError: method() takes 1 positional argument but 2 were given
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
TypeError: Takes 1 positional argument but two were given
Ok sorry for the mix-up
I looked it up yesterday- and literally smacked my head on the table.
I used this:def onclick(self):
And should have done this:def onclick(self, event):
Again, I'm sorry for not realizing this in advance.
takes 1 positional argument but 2 were given error for dict
U miss the self
argument in the function definition. Your Teacher class should look like this:
class Teacher(User):
def __init__(self, fisrtname, lastname, code):
super().__init__(fisrtname, lastname)
self.code = code
def addcoursess(self, item):
dict = {}
dict.update(item)
cs.append(dict)
print(dict)
def fullname(self):
print("i am an teacher")
super().fullname()
Or if u don't want to pass the self
, the class should be defined with the @staticmethod
decorator like this:
class Teacher(User):
def __init__(self, fisrtname, lastname, code):
super().__init__(fisrtname, lastname)
self.code = code
@staticmethod
def addcoursess(item):
dict = {}
dict.update(item)
cs.append(dict)
print(dict)
def fullname(self):
print("i am an teacher")
super().fullname()
Info about static method are here: https://docs.python.org/3/library/functions.html#staticmethod
how to make subclass :-TypeError: __init__() takes 1 positional argument but 2 were given
I have written a working example for you. The code has many comments as explanation and for better understanding.
Code:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def sup(self):
# Change the "print" to "return" because the print is on caller side.
return f"{self.name} {self.age} {self.language}"
# The "Subclass" is inherited from "Person"
class Subclass(Person):
def __init__(self, name, age, language):
# Call the __init__ method of parent class with the proper parameters
super().__init__(name, age)
# Set the "language" as instance variable.
self.language = language
# The input parameters are separated so "," is needed instead of "+"
sm = Subclass(input("input your name: "), input("input your age: "), input("input your language: "))
print(sm.sup())
Output:
>>> python3 test.py
input your name: Bill
input your age: 53
input your language: English
Bill 53 English
Notes:
I recommend to check the inheritation in Python: https://www.w3schools.com/python/python_inheritance.asp
The
super()
is Python3 specific: https://realpython.com/python-super/
pop() takes 1 positional argument but 2 were given for maximum stack problem
pop
does not take any additional arguments to self (which is the calling instance), so you may not provide any.
If you want pop(1000)
to do something you'll have to implement it as pop(self, your_param)
.
TypeError: pretty_print() takes 1 positional argument but 2 were given
In your code (t, 0, t)
is the second argument to pprint
(which takes only one positional argument), but it's meant to be the second argument for integrate
.
BTW, an integral from 0 to t
dt
seems strange as it uses the same variable for both integration and the upper bound.
You can write this in a cleaner way:
printing.init_printing() # CALL this function
t, x = symbols('t x')
a=5
b=3
# Nice readable formula for the function we're integrating
func = (
1 + a/b + (a/b - 1) * sinh(a*t) * sin(b*t)
- (a/b + 1) * cosh(a*t) * cos(b*t)
+ sinh(a*t) * cos(b*t)
- a/b * cosh(a*t) * sin(b*t)
)
# Integrate `func` from 0 to some `x`
the_integral = integrate(func, (t, 0, x))
# Output the result
pprint(the_integral)
Note how putting the formula for the function in a separate variable func
and the integration result into the_integral
makes it harder to get lost in parentheses and pass (t, 0, t)
as the argument to pprint
instead of integrate
.
Related Topics
Is There a Standardized Method to Swap Two Variables in Python
How to Find the Cumulative Sum of Numbers in a List
Changing One Character in a String
How to Hide Output of Subprocess
Python Rounding Error With Float Numbers
Typeerror: 'List' Object Is Not Callable in Python
Why Does This Iterative List-Growing Code Give Indexerror: List Assignment Index Out of Range
How to Chain the Movement of a Snake'S Body
Python'S Many Ways of String Formatting - Are the Older Ones (Going to Be) Deprecated
How to Send a "Multipart/Form-Data" With Requests in Python
How to Select a Variable by (String) Name
Pip Install' Fails For Every Package ("Could Not Find a Version That Satisfies the Requirement")
Are List-Comprehensions and Functional Functions Faster Than "For Loops"
Removing from a List While Iterating Over It