1

I'm trying to pass variable list in Class and Function in object oriented manner but facing some error, I don't understand what wrong with it and I'm using PyDev in Eclipse

CODE

class Mydetails:
    __details = ''

    def __init__(self,details):     # constructor
        #self.__details['country'] = details['country']
        self.__details = details

    def set_element(self,details):
        self.__details = details
    
    def get_list(self,details):
        return self.__details
 
details = ['ABC','DEF','GHI','JKL']
pdetails = Mydetails()
pdetails.set_element(details)
print(pdetails.get_list())

OUTPUT

Traceback (most recent call last):
  File "C:\workspace\python\pythonapp\list.py", line 18, in <module>
    pdetails = Mydetails()
TypeError: __init__() missing 1 required positional argument: 'details'
7
  • Please correct the formatting here. Python is highly dependent on indentation. Commented May 19, 2017 at 4:27
  • 1
    As an aside, this: __details = '' is almost not doing what you think it is. This creates a class-level variable, instead of an instance variable. You then shadow this variable on each instance in __init__ Commented May 19, 2017 at 7:05
  • @juanpa.arrivillaga exactly I'm shadowing variable values in self so that I can get it from get function. Commented May 19, 2017 at 8:41
  • @VrushalRaut What? No, you are shadowing a totally useless class-level variable that you create when you assign in the class block. You should just use self.details. See this question about class-level variables vs instance variables. Furthermore, you are then using double-underscore name-mangling mixed with getters and setters, not really the way things are done in Python. See this questions for more about that Commented May 19, 2017 at 8:48
  • @juanpa.arrivillaga I understand what you saying but it just prototype so I'm making base model in OOP so that later on I can do my project code on that basis , right now I'm passing variable of array(list) may be in future it will multiple variables so thats why getter and setter use so that "self" variable sound efficient when get function is call. Commented May 19, 2017 at 8:55

2 Answers 2

6

in this line:

def __init__(self,details):  

you made the init function take a parameter (details), but here:

pdetails = Mydetails()  

you arent giving it a parameter! what you probably wanted to do there is:

pdetails = Mydetails(details)

on a side note your def get_list(self,details): function doesn't make sense, why are you passing it a parameter that doesn't get used?

Sign up to request clarification or add additional context in comments.

6 Comments

I'm passing parameters to constructor and function so first set the variable values and then get output from other function.
but you DIDN'T pass the parameter. you declared it, but then didn't actually pass it
I tried your method but still getting same error, so how to do that ?
you cant get the same error if you tried what i suggested
I want to make __details = '' protected thats why it is outside of __init__(self,details)
|
1

I fixed my code ,It was error in function def get_list(self,details): changed to def get_details(self): because details parameter already copied in self so it not allow the same thats why throwing error, but below code is fixed and work fine with function calling and defined object also correct from previous code.

CODE

class Mydetails:
    __details = ''     #protected


    def __init__(self,details={}):  # constructor
        self.__details = details


    def set_details(self,details):        # function
        self.__details = details

    def get_details(self):
        return self.__details

    def toString(self):
        return 'Welcome {}'.format(self.__details)


new_details = ['xyz','klm','nop']
pdetails = Mydetails()
pdetails.set_details(new_details)
print(pdetails.get_details())

OUTPUT

['xyz', 'klm', 'nop']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.