Say I have something like:

class name:
.....def __init__(self,a):
..........self.a = a
..........self.b = [0 for i in range(a)

Now lets say I wrote a python program that has a main function that prompts the user for a set of numbers in the range(a). How do I get that set of numbers in the variable list self.b without defining another method the in the name class to do so?

Member Avatar for masterofpuppets

well you could simply do this:

class Name:
    def __init__( self, a, listOfValues ):
        self.a = a
        self.b = [ i for i in listOfValues ]

def main():
    a = 5
    # Generate the userInput list
    #....
   
    # and create the new object by passing the range and the list to the constructor
    n = Name( a, userInput )

main()

Hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.