Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Use of Multiple Object
#1
I want to create two objects of a class. Using these two objects i want to input some numbers into a list which is a member of the class. After inputting data it will display the content of the list. Simple code. But the output was not which i want. I enclosed my python code and the output. Please tell me why the program is not working. I am new programmer in Python.
class Demo:
    arr = []
    n = 0

def __init__(self,s):
    self.n=s
def fill(self):
    print("Enter elements in array ")
    for i in range(self.n):
        x=input()
        self.arr.append(x)

def show(self):
    for i in range(self.n):
        print(self.arr[i])

obj1 = Demo(5)
obj2 = Demo(3)

obj1.fill()
obj2.fill()

print("Data from first Object")
obj1.show()
print("Data from second object")
obj2.show()
Output:
Enter elements in array  1 2 3 4 5 Enter elements in array  6 7 8 Data from first Object 1 2 3 4 5 Data from second object 1 2 3
Quote
#2
Please be more specific on your requirements.
Quote
#3
Also, there is problem with the indentation of your code. Please, fix it.
Quote
#4
The problem is the position of the second line.
In this way, *all* instances of the class will share the same array. So, the array of the second one starts with the same elements as the array of the  first one, as they are set for all instances.
Shift the line to the start of fill and try what happens.
Quote
#5
(Mar-08-2017, 04:18 AM)Larz60+ Wrote: Please be more specific on your requirements.

Already I am giving you the entire details. By the way my problem was solved. Credit goes to StackOverflow. Anyway Thanks...

(Mar-08-2017, 07:08 AM)buran Wrote: Also, there is problem with the indentation of your code. Please, fix it.

Sir, there is no indentation problem in my program. By the way my problem was solved. Credit goes to StackOverflow. Anyway Thanks...

(Mar-08-2017, 10:04 AM)merlem Wrote: The problem is the position of the second line.
In this way, *all* instances of the class will share the same array. So, the array of the second one starts with the same elements as the array of the  first one, as they are set for all instances.
Shift the line to the start of fill and try what happens.

Sir, its not the solution which you told me. By the way my problem was solved. Credit goes to StackOverflow. Anyway thanks.
Quote
#6
(Mar-08-2017, 11:43 AM)arup Wrote: Sir, there is no indentation problem in my program.

Obviously in the code, published in the first post class methods __init__, fill and show are not indented, i.e. they are on the same level as class Demo ...
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  connecting to the spreadsheet object inside multiple enterprise software apps ajetrumpet 0 164 Jan-19-2020, 03:36 PM
Last Post: ajetrumpet
  How to sharing object between multiple process from main process using Pipe Subrata 1 653 Sep-03-2019, 09:49 PM
Last Post: woooee
  Return Object Created from Multiple Classes emerger 3 641 Oct-18-2018, 02:14 AM
Last Post: emerger

Forum Jump:


Users browsing this thread: 1 Guest(s)