|
0
|
This is useful for making easy and human to write definition files for whatever use. For example, lets say you are writing a simulator and you need an easy, human way to define parts about a given world like light sources, obstacles, and dimensions. see the discussion for this example fleshed out in code.
For the example above, You might write something like the following:
If you were to then call something like World(file) in the runtime of your program where file is as follows: #dimensions(4, 5) newlightsource(1, 2) newlightsource(2, 3) newobstacle(0, 1) #You will end up with an instance of class World with the appropriate data, thus it is simple to create new ways to specify data for your program
Tags: files
|
5 comments
Add a comment
Sign in to comment
Download
Copy to clipboard

Oh my.
Is a rather long way to spell:
very true.. fortunately, it would only eat up a humanly unidentifiable amount of processor time unless the file was a ridiculous length. I made the change though
Can't get this example working. Sorry - I'm new to Python.
I get:-
And if i change the line so the function name "dimensions" is qualified by the class "World", then I get problems when trying to exec the text read from the file.txt:-
BTW I'm using Python 2.4
again, a typo on my end. It should have been self.dimensions instead of dimensions
the dimensions method is indeed a method of the class World, but Python doesn't have static methods like java. Plus, this method needs an instance of world in order to edit instance data like width and height.
the self keyword in Python is equivalent to the this keyword in Java, and just refers to the instance of the class that the method was called in.
Thanks. that did the trick cheers