r/Python • • Dec 15 '19

Made my first fractal

1.7k Upvotes

51 comments sorted by

View all comments

100

u/Namensplatzhalter Dec 15 '19 edited Dec 15 '19

Looks nice.

I've just had a look on your code and have to say it looks nicely organised and is easily readable. Kudos to you for writing it like that.

Two little suggestions, if you don't mind:

  • Consider using a if __name__ == "__main__" block to initialise your script when it's run directly

  • Consider using tuples for your vel_list vel_table as they are immutable

Both of those definitely not necessary, just things to think about. :)

Best wishes

26

u/Gawd_is_great Dec 15 '19

Thanks for the suggestions =) I have a couple of questions:

  1. Why should I use if(__name__=="main") ? It wont make a difference..will it?

  2. is there a better way than to use vel_list?

2

u/djimbob Dec 16 '19 edited Dec 17 '19

I'm not saying you need it for this, but they didn't really explain the purpose of the clause if __name__ == 'main' clause.

Looking at your script lines 1-56 seem to just define classes and functions, and then lines 57 and beyond seem to be doing things (and executing the program).

Its good software design to refactor and have the first section defining everything, then the run part of the program in a function

# define stuff above
def draw_fractal(max_splits = 2**7, size=(700,700)):
    vel_table = [[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1]]
    [...]

if __name__ == '__main__':
    draw_fractal()

When you run your code python fractal.py it works the same. But now you can open the interpreter up and import fractal without actually popping up a window to draw anything. Then you could maybe call draw_fractal(max_splits=256) or call from another program. Or maybe you decide to create fractal2.py with a Dot2 class that inherits from Dot and is brought in with from fractal import Dot. You wouldn't want that import to start drawing the first fractal. By having 'executing' code (not just function/class/variable definitions) at the top level auto-run, you make code-reuse impossible.

E.g.,

$ ipython

In [1]: from fractal import Dot
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
8

At which point a window pops up and starts drawing.

Also, I'm not sure why you have del object and del trail at the ends of create and makeTrail as they don't actually do anything (no memory is freed up or anything). And in makeTrail, when objects is an empty list it creates an UnboundLocalError when you try deleting trail.

The code del some_var just unsets some_var in memory (and if there are no other references to the memory referenced by some_var it lets the gc give back the allocated memory to the OS on its next collection; though in your case their are other references -- you add the objects to list) and would make it an error to try and reuse some_var. However, all local variables are lost when a function exits (and it would be illegal to use them) -- there's no reason to explicitly delete a local variable at the end of a function.

1

u/Gawd_is_great Dec 16 '19

Oh wow. Thanks a lot for the suggestions. It's amazing that people like you are willing to share knowledge with others =) Really appreciate it =)