Jump to content
  • Advertisement
Sign in to follow this  
Genjix

Pass python types in by reference

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've looked on [google] but I don't know how to pass in any fundamental data types in python by reference (its a generator if it helps).
def F(a):
        a = 110

a = 0
F(a)
print a

Share this post


Link to post
Share on other sites
Advertisement
The short answer is that you don't. What are you trying to achieve? There are other ways to do it (such as returning multiple values, altering a global, passing in a mutable container type, etc).


def F():
return 110

a = 0
a = F()
print a

Share this post


Link to post
Share on other sites
Unfortuantly there is no pass-by-reference available for basic types in python. What you can do is wrap it in a list instead. like this:

def F(a):
a[0] = 110

a = [0]
F(a)
print a

Share this post


Link to post
Share on other sites

def ForLoop():
for i in range(0,10)
yield i

def EventList():
yield "hello"
yield "world"
yield "no no no"
yield 45

# pass in list and index, so I can
# remove from list
def ExecuteGenerator(generator):
if generator == None:
return

try:
# normally branch here
print generator.next()
except StopIteration:
generator = None

def Iterate(generator_list):
for it in generator_list:
ExecuteGenerator(it)

b = [ForLoop() , EventList()]

while(1):
Iterate(b)

# hopefully replace with if len(b) == 0
if b[0] == b[1] == None):
break


although in ExecuteGenerator() I'll just pass in the list and an index now.

Thanks for your help!

Share this post


Link to post
Share on other sites
Wow... I've no idea what you're actually trying to do, but I'm pretty sure that's not the best way to do it. If you're just trying to join 2 iterators/generators together in sequence, you probably want itertools.chain. I don't see why you need to empty the original list as you go along, instead of just iterating over it and discarding it at the end.

Share this post


Link to post
Share on other sites
well it's not just two generators... I plan to have more, this was just an example.

Also the point is, that some generators finish before others and I want to remove them from the list without going into 3+ indentation, but I couldn't pass by reference.

Thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!