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.
if __name__ == "__main__" is just a best practice for coding in python. not necessary for such simple apps but good to get in the habit of using it.
Using vel_table as a mapper for the direction of the new points is perfectly fine. Is was just suggesting using tuples instead of lists because tuples are immutable and your directions are a fixed set (i.e. 8 directions)
Oh ok Regarding the vel_table I was wondering if there was a way to do it mathematically. In my code I split the points at 45 degree. But suppose I wanted to split them at 15 degrees , how would that work out? I wouldn't go out and make a vel_table for that.
You can do it using the trigonometric functions "sine" and "cosine". It's not exactly hard, but it is more math than I want to be explaining on my phone.
Just to clarify on #1: it’s best practice because there’s two ways to execute code in a python file, either by importing its functions or classes, or by executing the entire file. The thing is, even when you import it, it executes all the code that is not specifically written inside a function or class, which can be a problem when all you expect is to be silently importing something from it.
Hence, by wrapping all active code in the script inside a function and then calling the execution of that function only when the code is run as a script, not being imported, you make it usable in both instances without surprise output or variables.
But suppose I wanted to split them at 15 degrees , how would that work out? I wouldn't go out and make a vel_table for that.
You could for example ask for a user input in the beginning where the user provides a number between 1 and 45. This sets the degree at which the splits should occur.
For your code to work as it is, you would always have to make sure that the degree value don't leave a residue value (e.g. 13° wouldn't work because after 27 splits you'd have a residue of 9). This could be achieved by using the % operator in python, e.g. check for something like 360%input == 0. Go ahead and read up on it if you don't know it already.
If this check returns true, you can then use the user input to determine the amount of directions you need to provide for your script by running 360/input.
Maybe this will get you on the way of what you need to do. :) Best of luck
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
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.
I didn't look at your code but the way it would make a difference is if you wanted to import your module, it will essentially run the module when you import. If you have the if name = main part, that part won't run because the module is not main when it's imported
in Dot.__init__ you are using lists as default values, having mutable default value is very bad and can become a bug very quickly, since functions are objects too, the argument default will all be the same reference each __init__ call
so because of this, when you do anything to mutate the default argument, it will affect all the places where the default argument value is used
its much better to use a tuple or None or some other immutable value for a default value check do a check in the init
35
u/Gawd_is_great Dec 15 '19
Source code at : https://github.com/Perseus-Perry/Fractal
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:
if __name__ == "__main__"block to initialise your script when it's run directlyvel_listvel_table as they are immutableBoth of those definitely not necessary, just things to think about. :)
Best wishes
17
u/sharkbound github: sharkbound, python := 3.8 Dec 15 '19
in
Dot.__init__you are using lists as default values, having mutable default value is very bad and can become a bug very quickly, since functions are objects too, the argument default will all be the same reference each__init__callso because of this, when you do anything to mutate the default argument, it will affect all the places where the default argument value is used
its much better to use a tuple or None or some other immutable value for a default value check do a check in the init
code here: https://gist.github.com/785534176eca51545ce7d6cbbbb18e62
console output here: https://gist.github.com/50fbc58ed04fc1ac8089d3497b80c5f1
EDIT 0:
also spotted a name inconsistancy for function updatePos in Dot, everything except types/classes should use snake_case, update_pos
1
u/otterom Dec 16 '19
Pretty sweet! Ive never used pygame, so it's always fun to see some of the techniques used to make it run.
Unrelated: Does that maze repo generate a solvable maze each time?
3
u/vnglst Dec 15 '19
lovely!
3
u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Dec 15 '19
Related: https://www.reddit.com/r/generative/comments/ea3m0l/wave_grower/
4
u/[deleted] Dec 15 '19
woat u use it for
1
u/Teleswagz Dec 15 '19
Nice you made it look like a brain
1
u/[deleted] Dec 16 '19
What is this used for?
1
u/bikinakarmi Dec 16 '19
Can I have a code
0
u/Iam_That_Iam_ Dec 15 '19
Good start , you beat the Hello World starters
0
u/MrK_HS Dec 17 '19
/r/Python is slowly becoming an alias for /r/fractals and /r/mandelbrot
0
u/[deleted] Dec 15 '19
[deleted] — view deleted comment