Plazmican
Plazmican

Reputation: 59

E1120:No value for argument in constructor call

I'm pretty new to Python Classes, and I wanted to test a simple code:

class information:
  def __init__(self,fname,lname):
    self.fname = fname
    self.lname = lname
  def first(self):
    print ("First name :", self.fname)
  def last(self):
    print ("Last name :",self.lname)
firstname = information("Name")
lastname = information("Test")

However, Pylint gives me an error:

E1120:No value for argument 'lname' in constructor call (11,13)
E1120:No value for argument 'lname' in constructor call (12,12)

What's the problem here? I think I have defined lname as 'self.lname = lname"

Upvotes: 4

Views: 12485

Answers (2)

Enrique Fernandez
Enrique Fernandez

Reputation: 46

As Austin commented, the init is expecting a value for the parameter lname.

You seem to be new in python, so suggest you to use a tutorial like this to understand better the function parameters https://www.tutorialspoint.com/python/python_functions.htm

I'll try to sum that uo a bit. There are several ways for declaring function arguments:

  1. Positional (your case): the parameters are mandatory and must be caled in that order(*). For example:


    def f(arg1, arg2):
        print(arg1)
        print(arg2) 

    f(1, 2)
    # OUT: 1
    # OUT: 2

  1. Default arguments. They have a default value in case they are not explicitly setted by the user. They respect the order (*) and must go always after the positional ones. Example:


    def f(arg1, arg2="default"):
        print(arg1)
        print(arg2) 

    f(1)
    # OUT: 1
    # OUT: default

    f(1, 2)
    # OUT: 1
    # OUT: 2

  1. Variable lenght parameters (*args). They go after, and get all the parameters from that point into a tuple. They are marked with the symbol *. Example:


    def f(arg1, *args):
        print(arg1)
        print(args) 

    f(1)
    # OUT: 1
    # OUT: ()

    f(1, 2, 3, 4, 5)
    # OUT: 1
    # OUT: (2, 3, 4, 5)

  1. Keyword arguments (**kwargs). Like Get all the named parameters wich name is not a positional or default-value parameter into a dictionary. They are marked with **. Example:


    def f(arg1, **kwargs):
        print(arg1)
        print(kwargs) 

    f(1)
    # OUT: 1
    # OUT: {]

    f(1, a=2, b=3, c=4, d=5)
    # OUT: 1
    # OUT: {'a':2, 'b':3, 'c': 4, 'd:' 5}

(*) Parameters can be passed by name, so you can use the order you like this way. BUT if you pass a parameter by name, teh following parameters must be passed by name as well.

Upvotes: 1

Austin
Austin

Reputation: 26037

__init__() is expecting two positional arguments, but one is given.

See your __init__():

def __init__(self,fname,lname):

You should create object of the class and use this object to call functions of the class, like so:

class information:

  def __init__(self,fname,lname):
    self.fname = fname
    self.lname = lname

  def first(self):
    print ("First name :", self.fname)

  def last(self):
    print ("Last name :",self.lname)

obj = information("Name", "Test")   # <-- see two arguments passed here.
firstname = obj.first()
lastname = obj.last()

# First name : Name
# Last name : Test

Note: When you create an object of a class, the class's constructor is automatically called. Arguments you pass while creating object must match with the number of arguments in the constructor.

Upvotes: 3

Related Questions