我正在为Linux构建一个状态栏。当插入不同的键盘时,我已经制作了一个模块:
set initialTeck [eval exec cat [glob /sys/bus/usb/devices/*/product]]
set initialTeck [string match *Truly* $initialTeck]
set initialKB [exec xkb-switch]
dict set table samy 0 samy
dict set table samy 1 temy
dict set table temy 0 samy
dict set table temy 1 temy
dict set table gb 0 samy
dict set table gb 1 temy
proc init {} {
variable initialTeck
variable initialKB
set currentTeck [eval exec cat [glob /sys/bus/usb/devices/*/product]]
set currentTeck [string match *Truly* $currentTeck]
set currentKB [exec xkb-switch]
if {$initialTeck != $currentTeck} {
set initialTeck $currentTeck
nextKB $currentKB $initialTeck
}
}
proc nextKB { currentKB teck } {
variable table
exec [dict get $table $currentKB $teck]
}
while 1 {
init
after 1000}
泰米是我为“真正符合人体工程学的键盘”制作的.xkb布局文件,samy是标准GB键盘的自定义.xkb布局。Teck存储USB端口的状态和哈希表的dict。
显然,状态栏还有更多的模块,我已经使用了名称空间,但是随着项目的扩大,我决定使用OOP。同时,我需要将项目转换为Python代码,以便在开发人员之间共享。
目前,TclOO、Snit、Stooop、Incr-Tcl、Xotcl等都有很多扩展.因此,由于Incr类似于C++,是否有类似于Python的Tcl扩展?
发布于 2015-07-17 17:14:31
所有的Tcl系统在风格上都没有Python那么相似;语言是不同的,并且喜欢不同的思考问题的方式。这其中的一部分是语法(大多数Tcl程序员不太喜欢_字符!)但更重要的是,Tcl系统的工作方式不同:
最终的结果是,Tcl的OO系统都使对象的行为像Tcl命令(当然,这允许很大的灵活性),而不像Tcl的值(Tcl值往往是相当简单的透明数字、字符串、列表和字典)。这使得OO与Python中的OO有很大的不同。
我想,你可以在最简单的层面上假装,但一旦你开始做任何复杂的事情,你就会发现其中的差异。不过,要说得更多,我们需要挑选一个更具体的例子,供我们深入研究。
这是一些Python代码
class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
self.description = "This shape has not been described yet"
self.author = "Nobody has claimed to make this shape yet"
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self, text):
self.description = text
def authorName(self, text):
self.author = text
def scaleSize(self, scale):
self.x = self.x * scale
self.y = self.y * scale下面是TclOO中类似的类定义(我最喜欢的):
oo::class create Shape {
variable _x _y _description _author
constructor {x y} {
set _x $x
set _y $y
set _description "This shape has not been described yet"
set _author "Nobody has claimed to make this shape yet"
}
method area {} {
return [expr {$_x * $_y}]
}
method perimeter {} {
return [expr {2 * $_x + 2 * $_y}]
}
method describe {text} {
set _description $text
}
method authorName {text} {
set _author $text
}
method scaleSize {scale} {
set _x [expr {$_x * $scale}]
set _y [expr {$_y * $scale}]
}
}除了明显的语法差异(例如,Tcl喜欢大括号并将表达式放入expr命令中)之外,主要要注意的是,变量应该在类定义中声明,并且不需要传递self (它自动以命令[self]的形式出现)。
https://stackoverflow.com/questions/31462142
复制相似问题