How can I make a list defined in one method available in another method when both methods are in the same class?

hfdd hudfhs

I have two methods within the same class, and I want to make a list in one method available in the other method, how do I do this?

Here is my code:

class Solution:
    primes=[]

    def primeNumber(self,n):
        numbers = set(range(n, 1, -1))
        self.primes = []
        while numbers:
            p = numbers.pop()
            self.primes.append(p)
            numbers.difference_update(set(range(p * 2, n + 1, p)))

    def goodNumbers(self,f):
        self.primes.append(f)
        return self.primes

x=Solution()
print(x.primeNumber(100)) ##populate the list
print(x.goodNumbers(123456789)) ##return the list as populated by previous call, and with the long number appended

For some reason, self.primes does not take into account the long number appended in goodNumbers()- any idea why this is?

gimix

As it has already been said, you can use an instance variable self.primes instead of your local variable primes. This is useful if you plan to reuse the values.

If instead you simply want to use the values once, and you want goodNumbers to call primeNumber, then simply add return primes at the end of primeNumber (outside the while loop of course)

EDIT

Since the question has been modified:

I just saw your edited question. That won't work: if you're using self.primes for passing data between methods then you need to first create it, probably at object initialization time, then to populate it with a call to primeNumber, and then you can read/modify it in goodNumbers.

For example:

class Solution:
    primes=[]

    def primeNumber(self,n):
        numbers = set(range(n, 1, -1))
        self.primes = []
        while numbers:
            p = numbers.pop()
            self.primes.append(p)
            numbers.difference_update(set(range(p * 2, n + 1, p)))

    def goodNumbers(self,n):
        self.primes.append(7598347534)
        return self.primes

x=Solution
x.primeNumber(100) ##populate the list
x.goodNumbers(123456789) ##return the list as populated by previous call, and with the long number appended

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I combine both of my methods to just one method?

How can I call a method in another method in the same class?

How can i make a method available only from within the class

How can I call a method of one class to another class in android

How can I run one method of a class automatically, before any other class methods in Python, when a new class instance is created

How to get list of methods defined in another class called from a given method in Java

How do I make external methods available in another class?

How can I call any method inside a class depending on an input to another method in the same class?

How can i call a String from a method in a toSting() method (all methods are in same class)

How can i use a value defined in the main() method in another method

How do I access the variable of one method from another method within the same class C#?

How to Display Methods from a one Class to the Main Method of Another Class

SPRING MVC: Defined HttpSession in One Method is Not Available to Another Method

How can I use insert_id from one method in model in another method in the same model

How to define one method as both instance method and class method (sharing the same name), each with different arguments?

Calling a list from one method to another in the same class in Python

How can I test if a class method is calling another method from one of the classes's private objects

How do I make a program stop when one list or another list is the same as a third list?

How can I replace a variable in one method from another method

How can I make a WireBox injected dependency available to a constructor method?

How can I mock some method calls of a bean but use the defined bean in other method calls in the same test class?

Java Methods when working with Swing - It is possible to call event method in another event method in same class?

How do I make a method in a class change the variables in another class?

How to call a Method to another Method in the same class

How to call part of one method in another in the same class?

How to pass a variable from one method to another in the same class?

How can I use the method (helper) defined in a view in another view?

How do I invoke a main() method of one class in another class?

How can I pass the same object to another method of another object?

TOP Ranking

HotTag

Archive