Python Forum
# of Positional arguments to pass for creating an object?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
# of Positional arguments to pass for creating an object?
#1
I'm getting an error when trying to instantiate a class, where I pass two arguments, and the third one (for self) is implicit. When I only pass in one variable (hostname), it runs, but doesn't pass in the data to assign to the attributes as it should.

The code:
#!/usr/local/bin/python3.4

import re, requests, sys, json, time, logging, os, platform, pprint

###################################################
# Class Asset
class Asset(object):
    def __init__(self, hostname, **kwargs):
        #log.debug("hostname is " + hostname + ", kwargs is " + str(kwargs))
        setattr(self, "hostname", hostname)
        for key in kwargs:
            setattr(self, key, kwargs[key])
    def get(key):
        return self.key

    def dump(self):
        for attr in dir(obj):
            if hasattr( obj, attr ):
                print( "obj.%s = %s" % (attr, getattr(obj, attr)))

data = dict()
hostname = "this-host"
data["test1"] = "val1"
data["test2"] = "val2"
test_obj = Asset(hostname, data)
The result when running it:
$ ./test3.py
Traceback (most recent call last):
  File "./test3.py", line 25, in <module>
    test_obj = Asset(hostname, data)
TypeError: __init__() takes 2 positional arguments but 3 were given
Any thoughts on why I'm getting this error?
Reply
#2
second argument which you are giving should be keyword argument. that is the error. you are giving it as non keyword argument
Reply
#3
Thanks. I double checked my reference; I was using https://stackoverflow.com/questions/2466...-in-python as a model. I matched the syntax, and it works now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 800 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,857 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: missing 3 required positional arguments: wardancer84 9 10,660 Aug-19-2021, 04:27 PM
Last Post: deanhystad
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,799 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 4,166 May-15-2021, 04:15 PM
Last Post: Anldra12
  Possible to dynamically pass arguments to a function? grimm1111 2 2,123 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 5,525 Jan-18-2021, 12:08 PM
Last Post: Serafim
  Creating list of lists from generator object t4keheart 1 2,161 Nov-13-2020, 04:59 AM
Last Post: perfringo
  how to pass arguments between pythons scripts? electricDesire 2 2,109 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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