M
M
Mike Evstropov2016-01-15 09:58:35
Python
Mike Evstropov, 2016-01-15 09:58:35

How to organize automatic initialization of classes in Python?

Described classes, but maybe they don't need to be initialized at all? For example, how to access statics on pehape? Is there such a thing? It just turns out that I will use properties and methods, and I do not need class instances in the form of an object. Thank you!
By the way, I saw such a thing on pehape:
where the Core is first initialized, and then the call to the children is initialized when called and placed in the property array. But in Python, you can't push objects into lists or sets...

class Core{
  // Свойства - Классы API
  private $classes = ['class'     => 'ClassName'];
  // Созданные объекты
  private static $objects = array();
  
  // Магический метод, создает нужный объект API
  public function __get($name){
    // Если такой объект уже существует, возвращаем его
    if(isset(self::$objects[$name]))
      return(self::$objects[$name]);
    // Если запрошенного API не существует - ошибка
    if(!array_key_exists($name, $this->classes))
      return null;
    // Определяем имя нужного класса
    $class = $this->classes[$name];
    // Подключаем его
    include_once('class/'.$class.'.php');
    // Сохраняем для будущих обращений к нему
    self::$objects[$name] = new $class();
    // Возвращаем созданный объект
    return self::$objects[$name];
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-01-15
@Aroused

stackoverflow.com/questions/12179271/python-classm...
Why didn't you just say? I've been through so much...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question