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'
Community
  • 1
  • 1
Vrushal Raut
  • 1,050
  • 2
  • 15
  • 20
  • Please correct the formatting here. Python is highly dependent on indentation. – Arya McCarthy May 19 '17 at 04: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__` – juanpa.arrivillaga May 19 '17 at 07:05
  • @juanpa.arrivillaga exactly I'm shadowing variable values in self so that I can get it from get function. – Vrushal Raut May 19 '17 at 08: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](http://stackoverflow.com/questions/2714573/instance-variables-vs-class-variables-in-python) 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](http://stackoverflow.com/a/7456865/5014455) – juanpa.arrivillaga May 19 '17 at 08: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. – Vrushal Raut May 19 '17 at 08:55
  • @VrushalRaut I'm sorry, I am not following you. – juanpa.arrivillaga May 19 '17 at 08:57
  • @juanpa.arrivillaga Thanks for help checkout code I fixed, now it works fine. – Vrushal Raut May 19 '17 at 17:03

2 Answers2

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?

Nullman
  • 4,179
  • 2
  • 14
  • 30
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']
Vrushal Raut
  • 1,050
  • 2
  • 15
  • 20