mardi 17 février 2015

Instantiating views, models, and databases in MVC

I'm currently new to Python and am learning about the MVC architectural pattern. For simplicity sake and ease of explaining, let's say I have 3 classes. (Model, View, & Controller).



class Controller
def __init__(self):
self.view = View()
self.model = Model()

def some_function(self):
return self.model.add_things(1,2)

class Model
def __init__(self):
pass

def add_things(self, x, y):
return x + y

class View:
def __init__(self):
pass


Is it normal practice to instantiate my views and models as instance variables for the Controller as above?


Or is it better to import the files manually (i.e. import Model) at the top of Controller file and call the methods directly in the controller?



from Model import Model
from View import View

class Controller
def __init__(self):
pass

def some_function(self):
return Model.add_things(1, 2)

class Model
def __init__(self):
pass

def add_things(self, x, y):
return x + y

class View:
def __init__(self):
pass




Aucun commentaire:

Enregistrer un commentaire