Possible Duplicates:
Python: Why can't I modify the current scope within a function using locals()?
How do you programmatically set an attribute in Python?
i know nammed dynamicly an varial in python with:
var = "my_var"
locals()[var] = "coucou"
print my_var
>>coucou
But i don't know how to make it on a variable class. When i try:
class Controller(object):
def __init__(self):
var = 'ma_variable_dynamique'
locals()[var] = 'toto'
print ma_variable_dynamique
The exeception "NameError: global name 'ma_variable_dynamique' is not defined" is raised.
Example in php:
class Users extends Controller{
public $models = array(‘User’);
function login(){
$this->User->validate();
}
//Call in contructor
function loadModel(){
foreach($this->models as $m){
$_class = $m.’Model’;s
$this->{$m} = new $_class();
}
}
}
Thx