[Tutor] Everything in one file?

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jul 18 12:00:55 EDT 2018


On 18/07/18 14:46, Shall, Sydney wrote:

>  > is no need to put every class in a separate file, and it usually leads
>  > to complicated dependencies and circular imports.

> I have constructed a program which has a parent class and a child class 
> and I have them in two different files. I import the parent class into 
> the child class and create instances with the child class. This works fine.
> But, would it be better to have both classes in one file? 

Probably if you know when writing them that you will need to
import parent to use child and especially if you are unlikely
to ever use parent without child(or some other child class).

> Would then the 
> child class 'know' that the parent class is in the file?

Python visibility is controlled by the scope rules (and some other magic
stuff that applies to packages but needn't concern us here!)

The scope that we are concerned with is module scope,
sometimes called global scope, but not in the normal sense
that global has in other languages. Module or global scope
means that everything inside a module is visible to
everything else inside the same module.

So yes you child class can see the parent class if
it is in the same file (aka module).

# myfile.py ##########

class Parent: pass

class Child(Parent): pass

# EOF #############

So Parent is visible to the child definition without any
extra work. Now consider the 2 file option:

# parent.py #####

class Parent: pass

# EOF ######


# child.py ###

import parent
class Child(parent.Parent): pass

# EOF ######

Now the child class must import parent and dereference
Parent within its module. (ie use parent.Parent)

So two classes is more work for you and more work
for the interpreter.

> And further. I have a function which sets up all the suitable data 
> structures into which this function will put the information supplied by 
> the user. I import this file too into both the parent and the child class.
> Would the instances with which I actually work, 'know' that this 
> function is there if it is simply present in the same file?

Yes, again the function definition would have module/global
scope so the classes can see the function with no extra work.
The instances don;t see the function idf they are in a separate
file but they do see their class definition which includes all
the methods. And those methods see the function.
Like so:

# myfile2.py ##########

def myFunc():
    return (1,2,3)  # some data structure...

class Parent: pass

class Child(Parent):
   def __init__(self):
      self.data = myFunc() # access the function

# EOF #############

# myinstance.py ###

import myfile2

instance = myfile2.Child()

print(instance.data)  # -> (1,2,3)

# EOF ####

So it works.

But it's probably bad practice. Classes should do
all their own data maniplulation. Having a function
outside the class preparing data structures for use
by a class is bad OOP practice. Much better to
prepare the data structures inside the class
 - maybe as a method of Parent?

Remember classes are data structures too. That's why
its called object oriented programming - you define
objects and pass the objects around. You don't pass
data between objects.  You pass objects.
Objects are data, and they know how to process their
own data - so nobody else should need to.
That's called encapsulation and is the basis of all
things OOP.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list