English 中文(简体)
进口类别及其从另一个档案中的功能
原标题:importing class and its function from another file
  • 时间:2010-06-01 10:38:03
  •  标签:
  • python
  • class

我对花on的进口班没有什么问题。 我的工作流程也好像这样。

index.py

    class Template:

        def header():
        def body():
        def form():
        def footer():

display.py

我愿在我的<编码>上打上<条码>标题(、<条码>和<条码>脚(>)。 任何人都会在晚宴上表明这一问题。 感谢你的关切。

指数文档-[Index.py]***

http://pastebin.com/vRsJumzq“rel=”http://pastebin.com/qNB53KTE和显示.py_

最佳回答

在您的指数档案底部,你创建了的标语,并称其所有方法。 由于该守则不包含在任何其他部分,因此在你进口该单元时就已执行。 你们要么需要去除,要么检查档案是否从指挥线操作。

if __name__ == "__main__":
    objx=HtmlTemplate()
    objx.Header()
    objx.Body()
    objx.Form()
    objx.Footer()
    objx.CloseHtml()
问题回答

你尝试了什么? 下面是进口后使用<代码>Template类方法的正常方式。

from index import Template

t = Template()
t.header()
t.body()
t.footer()

<<>t/strong>,在index.py文档(第99-105条)末尾,请你再从上述界定的<代码>Template类别中标明所有职能。 因此,你看到重复。

<><>Edit>: 根据你的法典,我看到你的问题是什么。

阁下:

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
objx.Header()
objx.Body()
objx.Form()
objx.Footer()
objx.CloseHtml()

之后在<代码>上,py:

t = HtmlTemplate()
t.Header()
t.Body()

Body()两次被点问?

作为脚注,你应当使用下限方法名称和上课时的资本字。 它是一个好公约。 我非常建议。

您仅应在<条码>上打造该物体,并称之为所有方法。

我不清楚我是否正确理解你,但我认为你正在问,如何在另一个文字中进口<代码>template。 www.un.org/Depts/DGACM/index_french.htm 声明是你需要的:

from index import template

foo = template()

foo.header()
foo.body()
foo.footer()

页: 1

cgitb.enable()
print  Content-type: text/html

 
print "<link rel="stylesheet" type="text/css" href="css/msa.css" >"

# [...]

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
# [...]      
objx.CloseHtml()

每次请上<编码>进口指数。

防止这种情况发生,把它 in成一个障碍:

if __name__ ==  __main__ :
    cgitb.enable()
    print  Content-type: text/html

 
    print "<link rel="stylesheet" type="text/css" href="css/msa.css" >"

    # [...]

    ## Calling all the functions of the class template with object (objx)
    objx=HtmlTemplate()
    # [...]      
    objx.CloseHtml()

......or better , 仍可从别处索取该守则的功能

以下解决办法对我有利:

class1(unittest.TestCase):
   def method1(self)

class2(unittest.TestCase):
   def method2(self):
      instance_name = class1("method1")
      instance_name.method1()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签