Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:58:14+00:00 2026-06-04T01:58:14+00:00

So I have some code that is being somewhat troublesome to rewrite: def func_A():

  • 0

So I have some code that is being somewhat troublesome to rewrite:

def func_A():

    x=[]
    # the function picks random members of the local x and rearranges them,
    # after giving x some initial value
    return x

Now I want to create an iteration in a func_B such that, for each run-through in an interation, func_B uses the x value that func_A generates to do things.

However, I want to avoid making x a global variable.

My first thought was to make the first line of the def of func_B x=func_A(), but this would run the randomizer twice and the value of x would be changed.

How do I reference the same x that func_A produces in func_B without running the randomizer again (until the next iteration in func_B) and without using global variables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:58:16+00:00Added an answer on June 4, 2026 at 1:58 am

    Use Classes

    import random
    class Randomizer():
        def __init__(self):
            # Generates a list of random values
            self.x = []
            self.rng = 10
            for i in range(1,self.rng+1):
                self.x.append(random.random())            
    
            # Need to call once to make sure all vars defined
            self.func_A()
    
        # pick a random value from a list
        def func_A(self):
            self.recent = self.x[random.randint(0,self.rng-1)]
            return self.recent
    
        # Do something with random value picked by A
        def func_B(self):
            return self.recent*self.recent
    

    Think this is what you meant?

    >>> r = Randomizer()
    >>> r.func_A()
    0.91223345147715629
    >>> r.func_B()
    0.83216986999392528
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have some code that is being repeated over and over, and it
I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
I have some code that looks like this (I left out the parts that
i have some code that tries to list images based on their height. the
I have some code that creates a new event source: EventLog.CreateEventSource(Source, LogName); I know
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that generates image of a pie chart. It's a general

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.