开发者

translating a ruby script to python

开发者 https://www.devze.com 2023-03-02 04:44 出处:网络
my problem is the following i have a ruby script looking like this module parent module son_1 ... some code here

my problem is the following

i have a ruby script looking like this

module parent

    module son_1
        ... some code here
    end

    module son_2
        ... some code here
    end

    class one
        ... some code here
    end

    class two
        ... some code here
    end

end

and i need this script to be translated to python but i am little confused ?

first i made this :

  • i turned the "module parent" into a python package

  • i made "module son_1" and "module son_2" like two files inside the package

  • and finally i defined the last two classed in the __init__ file of this package (开发者_如何学编程 "module parent" )

my questions are :

  • is this solution correct ?

  • and if it is , is there a better one ?


It should look like this on the file system :

- parent/
  ------- __init__.py << empty file or can have classes if you need, that way python will treat parent as a package
  ------- son_1.py
  ------- son_2.py
- test.py

then in actual code in test.py :

from parent import son_1, son_2, one, two
c = one('something')
b = son_1.something()
#or
import parent
import parent.son_1
import parent.son_2
c = parent.one('something')
b = parent.son_1.something()
0

精彩评论

暂无评论...
验证码 换一张
取 消