Couple of noobish question

Gary Herron gherron at islandtraining.com
Wed Feb 4 12:26:28 EST 2009


Catherine Heathcote wrote:
> Firstly hi, I don't know any of you yet but am picking up Python and
> will be lurking here a lot lol. I am a hobbiest coder (did 3 out of 4
> years of a comp tech degree, long story) and am learning Python, 'cos
> I saw some code and it just looks a really nice language to work with.
> I come from C++, so I am bound to trip up trying to do things the
> wrong way!

Welcome.  I suspect you'll enjoy Python.  (Far more than C++ ).
>
> I have been working with Project Euler to get the hang of Python, and
> all goes well. I have an idea for a small project, an overly
> simplistic interactive fiction engine (well more like those old choose
> your own adventure books, used to love those!) that uses XML for its
> map files. The main issues I see so far is the XML parsing (I should
> pick that up ok, I have a blackbelt in google-foo), but more
> importantly splitting code files.

Several modules exits to do the parsing of XML:  elementtree, xml, and
beautifulsoup come to mind immediately.
>
> In C++ I would obviously split .cpp and .h files, pairing them up and
> using #include. How do I do this in Python? I see that you don't tend
> to split logic from defenition, but how do I keep different classes in
> different files? My google-fu fails me so far.

Use the import statement for this.

If file a.py defines some classes or functions
a.py:
  class UsefulClass:
    ...
  def UsefulFn(...):
    ...

Then your main Python file imports it and uses the things define in a.py
like this:
  import a
  ob = UsefulClass(...)
  a.UsefulFn()

Good luck,

Gary Herron


> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list