Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError in Python 3.x

I have no idea what is wrong! This is a very simple program and I have done a lot head banging! Please someone enlighten me!

This a lab problem from the CSE 111 - Programming Language II course. They teach Java at the university and the code I wrote in Java works fine.

I just have to create a Student class with some fields to hold the basic information about a student with methods to get and set the attributes. Then create an instance of that class and tryout the methods. But every time I run this program the following error occurs:

TypeError: set_name() takes exactly 1 positional argument (2 given)

Here is the code I wrote.

class Student:
    '''Student class'''

    name = None
    id = 0
    address = None
    cgpa = None

    def get_name():
        return name

    def set_name(n):
        name = n

    def get_id():
        return id

    def set_id(i):
        id = i

    def get_address():
        return address

    def set_address(a):
        address = a

    def get_cgpa():
        return cgpa

    def set_cgpa(c):
        cgpa = c


#An object of Student class
jack = Student()

jack.set_name('jacky')
print(jack.get_name())
like image 816
ratulotron Avatar asked Feb 19 '26 22:02

ratulotron


2 Answers

You're not accepting a reference to your instance as the first argument to that method, i.e. your set_name() should be written:

def set_name(self, n):
    self.name = n

This is somewhat different from other languages where there is a built-in keyword (such as this) that refers to the current object. Python passes that reference explicitly, as an argument to the method.

All your other methods must be modified similarly.

Note that just setting name = n sets a local variable name which goes away when the method ends; it does not set anything on the instance. You have to explicitly set self.name if you want an instance attribute.

Also, and this is a matter of style, but you do not usually write set and get methods in Python. It is normal practice to set and get attributes directly. If you want to do validation of values, use a property instead. So basically, none of your methods are actually necessary in good style.

However, you don't have an __init__() method. Usually you would pass the desired attributes of the instance when instantiating the class and save these on the instance.

class Student:

    def __init__(self, name, id, address, cgpa):
        self.name    = name
        self.id      = id
        self.address = address
        self.cgpa    = cgpa

herman = Student("Herman Munster", 12345, "1313 Mockingbird Lane", 4.0)
like image 158
kindall Avatar answered Feb 22 '26 12:02

kindall


Try this:

import sys 
class Student:
   '''Student class'''

   self.name = None
   self.id = 0
   self.address = None
   self.cgpa = None

   def get_name(self):
       return self.name

   def set_name(self, n):
       self.name = n

   def get_id(self):
       return self.id

   def set_id(self, i):
       self.id = i

   def get_address(self):
       return self.address

   def set_address(self, a):
       self.address = a

   def get_cgpa(self):
       return self.cgpa

   def set_cgpa(self, c):
       self.cgpa = c
like image 40
barendt Avatar answered Feb 22 '26 10:02

barendt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!