Python Forum
Why am I getting this error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why am I getting this error?
#1
I'm trying to learn how to use Python classes using this one as an example:

class ExampleClass:
...     def __init__(self,example_parameter):
...         self.example_data=33
...         self.example_param=example_parameter
...         print("message from the constructor")
...     def example_method(self):    
...         print(self.example_param)
...     def get_example_data(self):    
...         return self.example_data
...     def set_example_data(self):   
...         self.example_data=value
I keep getting this error message for this functions:
example_class_object.set_example_data(543)
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
TypeError: ExampleClass.set_example_data() takes 1 positional argument but 2 were given
Reply
#2
The first argument of any method is the object itself (which is usually termed self in the argument list). Any other arguments you give come after that.

So when you do:
example_class_object.set_example_data(543), that gets called as:

set_example_data(example_class_object, 543)

But your definition line for the method only has one argument, not two. It also later tries to assign value, but value has not been assigned by anything. Presumably you want the definition to read:
    def set_example_data(self, value):
the_jl_zone likes this post
Reply
#3
Try when post longer code,and not post the code/copy from interactive shell.
Then we have to clean up ... before can test it,
or there are shell that can paste this code with ... like Ipython, ptpython.
But not all use theses shell,now have gotten answer here is what i mean how code should look.
class ExampleClass:
    def __init__(self, example_parameter):
        self.example_data = 33
        self.example_param = example_parameter
        print("Message from the constructor,or more correct initializer")

    def example_method(self):
        print(self.example_param)

    def get_example_data(self):
        return self.example_data

    def set_example_data(self, value):
        self.example_data = value
Now to test can use interactive shell,if it's just few lines.
>>> obj = ExampleClass(999)
Message from the constructor,or more correct initializer
>>> obj.set_example_data(123)
>>> obj.get_example_data()
123
the_jl_zone likes this post
Reply
#4
The code you provided defines a class called ExampleClass with an initializer, instance variables, and methods. Here's the code with some corrections to address the syntax errors:

class ExampleClass:
def __init__(self, example_parameter):
self.example_data = 33
self.example_param = example_parameter
print("Message from the constructor")

def example_method(self):
print(self.example_param)

def get_example_data(self):
return self.example_data

def set_example_data(self, value):
self.example_data = value

In this code, the ExampleClass has an initializer __init__() that takes example_parameter as an argument. It initializes two instance variables: example_data with the value 33 and example_param with the value of the example_parameter argument.

The class also has three methods:

example_method() simply prints the value of example_param.
get_example_data() returns the value of example_data.
set_example_data(value) sets the value of example_data to the provided value.
You can now create an instance of the class and call its methods. For example:

example_instance = ExampleClass("Hello")
example_instance.example_method()
print(example_instance.get_example_data())
example_instance.set_example_data(42)
print(example_instance.get_example_data())

example_instance = ExampleClass("Hello")
example_instance.example_method()
print(example_instance.get_example_data())
example_instance.set_example_data(42)
print(example_instance.get_example_data())
Reply


Forum Jump:

User Panel Messages

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