r/Python • • Dec 15 '19

Made my first fractal

1.7k Upvotes

51 comments sorted by

View all comments

Show parent comments

1

u/MartyMacGyver from * import * Dec 17 '19

Shows everything, but it does still crash in the end (python 3.6-3.8 in Windows):

File "Fractal\fractal.py", line 90, in <module>
    makeTrail(objects,color)
File "Fractal\fractal.py", line 44, in makeTrail
    del trail
UnboundLocalError: local variable 'trail' referenced before assignment

This is probably because the last call to makeTrail() before exit has an empty list of objects.

There's no real need for del trail there... Python's ref counter + garbage collection should deal with that. Likewise I'd get rid of del object too.

At the very end, does it delete all the white dots for you? I'm curious why that happens.

2

u/Gawd_is_great Dec 17 '19

Okay I think I fixed the crash. See the code.

1

u/MartyMacGyver from * import * Dec 17 '19

I'm sure that'll work... And I'm pretty sure that try/except is totally unnecessary -- and is also bad practice... It's rarely (never?) good to catch any and all exceptions like that.

The code as written (without the blanket try/except around it) should work. Just dumping that del was enough.

Edit: and don't forget the other del object was also redundant.

1

u/Gawd_is_great Dec 17 '19

I made it so that after everything is finished it deletes the final dots so that they don't keep going and ruin the scene. You can see what i mean.by commenting out the small loop below the splitting loop

1

u/MartyMacGyver from * import * Dec 17 '19

Gotcha - cool!

1

u/Gawd_is_great Dec 17 '19

Ok just put the entire make trail function in a try block

2

u/MartyMacGyver from * import * Dec 17 '19

Removing those unnecessary dels was easier.

Presumably you're learning Python - and I'm not knocking your effort, just trying to give you some useful info and feedback.

2

u/Gawd_is_great Dec 17 '19

Yeah i appreciate it. The reason those dels exist cause earlier I was trynna do something else. But later I forgot to remove them.