我正在构建一个重用和简单的ORM库; 一切都很顺利,除了我被一个愚蠢的继承限制所困扰.请考虑以下代码:
class BaseModel {
/*
* Return an instance of a Model from the database.
*/
static public function get (/* varargs */) {
// 1. Notice we want an instance of User
$class = get_class(parent); // value: bool(false)
$class = get_class(self); // value: bool(false)
$class = get_class(); // value: string(9) "BaseModel"
$class = __CLASS__; // value: string(9) "BaseModel"
// 2. Query the database with id
$row = get_row_from_db_as_array(func_get_args());
// 3. Return the filled instance
$obj = new $class(); …Run Code Online (Sandbox Code Playgroud) 我目前正在构建一个必须将文件名与模式匹配的工具.为方便起见,我打算提供延迟匹配(以类似glob的方式)和regexp匹配.例如,以下两个片段最终会产生相同的效果:
@mylib.rule('static/*.html')
def myfunc():
pass
@mylib.rule(r'^static/([^/]+)\.html')
def myfunc():
pass
Run Code Online (Sandbox Code Playgroud)
AFAIK r''仅对Python解析器有用,它实际上str在解析后创建了一个标准实例(唯一的区别在于它保留了\).
有人知道一种方法来告诉彼此吗?
我不想为同一目的提供两个备用装饰器,或者更糟糕的是,手动解析字符串以确定它是否是正则表达式.