In a [previous lesson](./Beginner-Tutorial-Python-basic-introduction.md#importing-code-from-other-modules) we already learned how to import resources into our code. Now we'll dive a little deeper.
No one writes something as big as an online game in one single huge file. Instead one breaks up the code into separate files (modules). Each module is dedicated to different purposes. Not only does it make things cleaner, organized and easier to understand.
Splitting code also makes it easier to re-use - you just import the resources you need and know you only get just what you requested. This makes it easier to spot errors and to know what code is good and which has issues.
To reiterate, the _python_path_ describes the relation between Python resources, both between and inside Python _modules_ (that is, files ending with .py). Paths use `.` and always skips the `.py` file endings. Also, Evennia already knows to start looking for python resources inside `mygame/` so this should never be included.
> Avoid renaming unless it's to avoid a name-collistion like above - you want to make things as easy to read as possible, and renaming adds another layer of potential confusion.
If you find entering multiple lines in the `py` command clunky (a traditional mud client is pretty limited for this kind of thing) you can also `cd` to your `mygame` folder and run `evennia shell`. You will end up in a python shell where Evennia is available. If you do `pip install ipython` you'll get an even more modern python shell to use. This works outside the game but `print` will show in the same way.
```
The same goes when writing code in a module - in most Python modules you will see a bunch of imports at the top, resources that are then used by all code in that module.
A docstring is not the same as a comment (created by `#`). A docstring is not ignored by Python but is an integral part of the thing it is documenting (the module and the class in this case). For example, we read docstrings to help text for [API documentation](../../../Evennia-API.md); we could not do that with comments.
The real file is much longer but we can ignore the multi-line strings (`""" ... """`). These serve as documentation-strings, or _docstrings_ for the module (at the top) and the `class` below.
The `class` named `Script`_ inherits_ from `DefaultScript`. As you can see `Script` is pretty much empty. All the useful code is actually in `DefaultScript` (`Script`_inherits_ that code unless it _overrides_ it with same-named code of its own).
We need to do a little detour to understand what a 'class', an 'object' or 'instance' is. These are fundamental things to understand before you can use Evennia efficiently.
Classes, objects, instances and inheritance are fundamental to Python. This and some other concepts are often clumped together under the term Object-Oriented-Programming (OOP).
A 'class' can be seen as a 'template' for a 'type' of object. The class describes the basic functionality of everyone of that class. For example, we could have a class `Monster` which has resources for moving itself from room to room.
- An `object` is an `instance` of a `class`. Like using a mold to cast tin soldiers, one class can be `instantiated` into any number of object-instances. Each instance does not need to be identical (much like each tin soldier can be painted differently).
> Note how we _didn't_ call the method as `fluffy.move_around(self)`. While the `self` has to be there when defining the method, we _never_ add it explicitly when we call the method (Python will add the correct `self` for us automatically behind the scenes).
We now have two monsters and they'll hang around until with call `quit()` to exit this Python
instance. We can have them move as many times as we want. But no matter how many monsters we create, they will all show the same printout since `key` is always fixed as "Monster".
The `__init__` is a special method that Python recognizes. If given, this handles extra arguments when you instantiate a new Monster. We have it add an argument `key` that we store on `self`.
Or you can use a separate terminal and restart from outside the game:
```{sidebar} On reloading
Reloading with the python mode gets a little annoying since you need to redo everything after every reload. Just keep in mind that during regular development you will not be working this way. The in-game python mode is practical for quick fixes and experiments like this, but actual code is normally written externally, in python modules.
The difference between the function and an instance of a class (the object), is that the object retains _state_. Once you called the function it forgets everything about what you called it with last time. The object, on the other hand, remembers changes:
The `fluffy` object's `key` was changed for as long as it's around. This makes objects extremely useful for representing and remembering collections of data - some of which can be other objects in turn. Some examples:
Classes can _inherit_ from each other. A "child" class will inherit everything from its "parent" class. But if the child adds something with the same name as its parent, it will _override_ whatever it got from its parent.
We added some docstrings for clarity. It's always a good idea to add doc strings; you can do so also for methods, as exemplified for the new `firebreath` method.
We created the new class `Dragon` but we also specified that `Monster` is the _parent_ of `Dragon` but adding the parent in parenthesis. `class Classname(Parent)` is the way to do this.
It's possible to add more comma-separated parents to a class. We show an example of such 'multiple inheritance' last in this lesson. You should usually avoid yourself setting up multiple inheritance until you know what you are doing. A single parent will be enough for almost every case you'll need.
Because we didn't (re)implement `__init__` in `Dragon`, we got the one from `Monster`. We did implement our own `move_around` in `Dragon`, so it _overrides_ the one in `Monster`. And `firebreath` is only available for `Dragon`s. Having that on `Monster` would not have made much sense, since not every monster can breathe fire.
One can also force a class to use resources from the parent even if you are overriding some of it. This is done with the `super()` method. Modify your `Dragon` class as follows:
> Keep `Monster` and the `firebreath` method. The `# ...` above indicates the rest of the code is unchanged.
The `super().move_around()` line means that we are calling `move_around()` on the parent of the class. So in this case, we will call `Monster.move_around` first, before doing our own thing.
We can see that `Monster.move_around()` is called first and prints "Smaug is moving!", followed by the extra bit about the trembling world from the `Dragon` class.
Inheritance is a powerful concept. It allows you to organize and re-use code while only adding the special things you want to change. Evennia uses this a lot.
Open `mygame/typeclasses/objects.py` in your text editor of choice.
```python
"""
module docstring
"""
from evennia import DefaultObject
class ObjectParent:
"""
class docstring
"""
pass
class Object(ObjectParent, DefaultObject):
"""
class docstring
"""
pass
```
In this module we have an empty `class` named `ObjectParent`. It doesn't do anything, its only code (except the docstring) is `pass` which means, well, to pass and don't do anything. Since it also doesn't _inherit_ from anything, it's just an empty container.
The `class` named `Object`_ inherits_ from `ObjectParent` and `DefaultObject`. Normally a class only has one parent, but here there are two. We already learned that a child inherits everything from a parent unless it overrides it. When there are more than one parents ("multiple inheritance"), inheritance happens from left to right.
So if `obj` is an instance of `Object` and we try to access `obj.foo`, Python will first check if the `Object` class has a property/method `foo`. Next it will check if `ObjectParent` has it. Finally, it will check in `DefaultObject`. If neither have it, you get an error.
Why has Evennia set up an empty class parent like this? To answer, let's check out another module, `mygame/typeclasses/rooms.py`:
```python
"""
...
"""
from evennia.objects.objects import DefaultRoom
from .objects import ObjectParent
class Room(ObjectParent, DefaultRoom):
"""
...
"""
pass
```
Here we see that a `Room` inherits from the same `ObjectParent` (imported from `objects.py`) along with a `DefaultRoom` parent from the `evennia` library. You'll find the same is true for `Character` and `Exit` as well. These are all examples of 'in-game objects', so they could well have a lot in common. The precense of `ObjectParent` gives you an (optional) way to add code that _should be the same for all those in-game entities_. Just put that code in `ObjectParent` and all the objects, characters, rooms and exits will automatically have it as well!
We will get back to the `objects.py` module in the [next lesson](./Beginner-Tutorial-Learning-Typeclasses.md).
We have created our first dragons from classes. We have learned a little about how you _instantiate_ a class into an _object_. We have seen some examples of _inheritance_ and we tested to _override_ a method in the parent with one in the child class. We also used `super()` to good effect.
We have used pretty much raw Python so far. In the coming lessons we'll start to look at the extra bits that Evennia provides. But first we need to learn just where to find everything.