Skip to main content
 首页 » 编程设计

python之如果既不是按位运算也不是 print chevron,>> 是做什么的

2025年05月04日20jillzhang

有没有人可以帮助找出双大于号(即第 10 行的 Drug >> float 和第 13 行的 Drug >> int)在以下 python 脚本中意味着什么?

 1  from owlready2 import * 
 2 
 3  onto = get_ontology("http://test.org/onto.owl") 
 4 
 5  with onto: 
 6      class Drug(Thing): 
 7          def get_per_tablet_cost(self): 
 8              return self.cost / self.number_of_tablets 
 9 
10      class has_for_cost(Drug >> float, FunctionalProperty): 
11          python_name = "cost" 
12 
13      class has_for_number_of_tablets(Drug >> int, FunctionalProperty): 
14          python_name = "number_of_tablets" 
15 
16  my_drug = Drug(cost = 10.0, number_of_tablets = 5) 
17  print(my_drug.get_per_tablet_cost()) 
 
--- Below is the printing outcome. --- 
2.0 

代码复制自here并在 python 3 中运行。

这里的>>>符号不是按位运算符。 Python's bitwise operation ,比如 x >> y,要求 y 是一个整数。很明显,此处的代码 (Drug >> float) 并未将 Drug 右移 float 位。

虽然之前也有人问过类似的问题(参见 here 和 here ),但他们的回答大多指向所谓的 "print chevron"在 python 2 中,>> 将要打印的消息重定向到 >> 之后指定的类似文件的对象。它们不适用于我的问题,因为这里不是打印语句的情况。

为了追查上面代码中的>>,我将第10行的脚本修改为class has_for_cost(Drug >> list, FunctionalProperty)(即替换>> float with >> list) 然后再次运行代码。它引发了一个 AttributeError,如下所示:

AttributeError                            Traceback (most recent call last) 
<ipython-input-1-1be374b27b04> in <module>() 
      8             return self.cost / self.number_of_tablets 
      9  
---> 10     class has_for_cost(Drug >> list, FunctionalProperty): 
     11         python_name = "cost" 
     12  
 
~/anaconda3/lib/python3.6/site-packages/owlready2/prop.py in __init__(Prop, name, bases, obj_dict) 
    254 class ReasoningPropertyClass(PropertyClass): 
    255   def __init__(Prop, name, bases, obj_dict): 
--> 256     super().__init__(name, bases, obj_dict) 
    257  
    258     if (not Prop.namespace.world is owl_world): 
 
~/anaconda3/lib/python3.6/site-packages/owlready2/prop.py in __init__(Prop, name, bases, obj_dict) 
     78  
     79       if not range is False: 
---> 80         Prop.range.extend(range) 
     81  
     82       if not inverse_property is False: 
 
~/anaconda3/lib/python3.6/site-packages/owlready2/util.py in extend(self, l) 
     58   def append(self, x):          old = list(self); super().append(x)         ; self._callback(self._obj, old) 
     59   def insert(self, i, x):       old = list(self); super().insert(i, x)      ; self._callback(self._obj, old) 
---> 60   def extend(self, l):          old = list(self); super().extend(l)         ; self._callback(self._obj, old) 
     61   def remove(self, x):          old = list(self); super().remove(x)         ; self._callback(self._obj, old) 
     62   def __delitem__(self, i):     old = list(self); super().__delitem__(i)    ; self._callback(self._obj, old) 
 
~/anaconda3/lib/python3.6/site-packages/owlready2/prop.py in _range_changed(Prop, old) 
    143     for x in new - old: 
    144       if isinstance(x, ClassConstruct): x._set_ontology(Prop.namespace.ontology) 
--> 145       x2 = _universal_datatype_2_abbrev.get(x) or x.storid 
    146       Prop.namespace.ontology.add_triple(Prop.storid, rdf_range, x2) 
    147  
 
AttributeError: type object 'list' has no attribute 'storid' 

虽然有了追溯信息,我还是找不到答案。如果有人能帮助弄清楚 >>> 在代码中的作用,我将不胜感激。

请您参考如下方法:

Drug >> float 调用 Drug 的 __rshift__方法。该方法可以为所欲为;它不一定是二进制移位操作。

阅读 the owlready2 documentation显示 domain >> range 是一种创建 ObjectProperty 的奇特方式。