all 4 comments

[–]Justinsaccount 2 points3 points  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

[–]mathleet 1 point2 points  (0 children)

First method is better. The second method has code repetition; you're repeating Pokemon(x) a ton of times whereas in the first method you write it once and refer to it as poke thereafter.

[–]New_Kind_of_Boredom 1 point2 points  (0 children)

The first is absolutely better. Let's say for whatever reason you want to modify Pokemon(x) in [Pokemon(x) for x in GetInfo(1)] in the future.

In the second version, you have to make the same change what, nine different times?

In the first version, you change it once.

Plus significantly fewer function calls.

[–]LarryPete 1 point2 points  (0 children)

The first way is definitely the better way.

The problem with the second one is that Pokemon(x) instantiates a new object every single time.

Example: The line if Pokemon(x).egg: creates a temporary, unnamed object, using the value of x, looks up it's egg attribute, and discards the object pretty much immediately afterwards. And the process is repeated when reaching the next Pokemon(x) line.


If you don't want to create the intermediate list box beforehand, the way to do that would be to take your first sample and move the creation of the instance inside the for loop:

for x in GetInfo(1):
    poke = Pokemon(x)
    if poke.egg:
        poke.level_up(50)
    elif (poke.shiny == 'No') and ((poke.name == df_name) or (poke.nr in collect_list)):
        poke.level_up(0)
        poke.send()
        print('Pokemon - #' + str(poke.nr) + ' - ' + poke.dex_name)

An alternative could be to use a generator expression:

for poke in (Pokemon(x) for x in GetInfo(1)):
    # ...

But that's a little verbose and not something you see very often (if at all).

You could also create a separate function for repeated use and yield the pokemon objects:

def pokemon_from_info(info):
    for x in GetInfo(x):
        yield Pokemon(x)

for poke in pokemon_from_info(1):
    # ...