how to bring the list I created in class as a parameter in new function

I'd like to say my English is second language and sorry for any inconvenience to read or understand my post.

I will explain what kind of code I produced first, So I created my class function using the code below

class Histogram:
  def __init__(self, range , max_mark):
    self.__range = range 
    self.__max_mark = max_mark
    self.__occurrence_list = [0] * self.__range
  def get_range(self):
    return self.__range
  def set_range(self, value):
    self.__range = value
  def get_max_mark(self):
    return self.__max_mark
  def set_max_mark(self, value):
    self.__max_mark = value
  def get_occurrence_list(self):
    return self.__occurrence_list
  def append_marks(self, value):
    if value > self.__max_mark:
        return 'value should be bigger than max mark'
    else:
        new = self.__occurrence_list[value] += 1

And What I want to do is printing a new strings by using the list which I got from the code below.

For example, If I put

a1 = Histogram(10, 9)
a1.append_marks(8)
print(a1.get_occurrence_list())

the result is

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]

Now, I want to create a string which shows index number and how many numbers they have.

For example, the given list shows 0 in each index but not in index 8

So I would like to create..

0: 0
1: 0
2: 0
...
8: 1
9: 0

Something like this.

So now I have a question,

But if I introduce new function which I would say 'get_string()' and using two parameters, which would be 'self' and other value.

How can I recall the list I've got from the last to the new function? What would be the code after

def get_string(self, other):

If I want to create something like the string above?

2 answers

  • answered 2018-01-17 04:15 John H

    Your object's property will be available in your new method. Code below should print your list as you described.

    for index, item in self.__occurrence_list.enumerate():
        print (str(index) + ':' + str(item))
    

  • answered 2018-01-17 04:15 Poiz

    You may need to revisit the Method, append_marks(self, value). Ideally that Method should have read:

    def append_marks(self, value):
        if value > self.__max_mark:
            return 'value should be bigger than max mark'
        else:
            # YOU SHOULDN'T DO new = self.__occurrence_list[value] += 1 
            # RATHER JUST DO: self.__occurrence_list[value] += 1
            self.__occurrence_list[value] += 1
            # new = self.__occurrence_list[value] += 1 #<= WITHOUT new =...
    

    The get_string() Method could be implemented thus:

    def get_string(self):
        outString   = ""
        if self.__occurrence_list:
            for key, val in enumerate(self.__occurrence_list):
                outString += str(key) + ":" + str(val)  + "\n"
        return outString
    

    Thus your Class could look something like this:

    class Histogram:
        def __init__(self, range , max_mark):
            self.__range = range
            self.__max_mark = max_mark
            self.__occurrence_list = [0] * self.__range
    
        def get_range(self):
            return self.__range
    
        def set_range(self, value):
            self.__range = value
    
        def get_max_mark(self):
            return self.__max_mark
    
        def set_max_mark(self, value):
            self.__max_mark = value
    
        def get_occurrence_list(self):
            return self.__occurrence_list
    
        def append_marks(self, value):
            if value > self.__max_mark:
                return 'value should be bigger than max mark'
            else:
                self.__occurrence_list[value] += 1
    
        def get_string(self):
            outString   = ""
            if self.__occurrence_list:
                for key, val in enumerate(self.__occurrence_list):
                    outString += str(key) + ":" + str(val)  + "\n"
            return outString
    
    
    
    
    a1  = Histogram(10, 9)
    a1.append_marks(8)
    a1.append_marks(8)
    a1.append_marks(8)
    a1.append_marks(8)
    a1.append_marks(8)
    a1.append_marks(8)
    
    print(a1.get_occurrence_list())
    print(a1.get_string())
    

    Test Screenshot