Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3类在使用构造函数时返回null_Python_Python 3.x_Libreoffice_Libreoffice Calc_Libreoffice Macros - Fatal编程技术网

Python3类在使用构造函数时返回null

Python3类在使用构造函数时返回null,python,python-3.x,libreoffice,libreoffice-calc,libreoffice-macros,Python,Python 3.x,Libreoffice,Libreoffice Calc,Libreoffice Macros,我使用以下命令启动了Libre Office Calc: $ libreoffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" import uno # Class so I don't have to do this crap over and over again... class UnoStruct(): localContext = None reso

我使用以下命令启动了Libre Office Calc:

$ libreoffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

import uno

# Class so I don't have to do this crap over and over again...
class UnoStruct():
    localContext = None
    resolver = None
    ctx = None
    smgr = None
    desktop = None
    model = None
    def __init__(self ):
        print("BEGIN: constructor")
        # get the uno component context from the PyUNO runtime
        localContext = uno.getComponentContext()

        # create the UnoUrlResolver
        resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )

        # connect to the running office
        ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        smgr = ctx.ServiceManager

        # get the central desktop object
        desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

        # access the current writer document
        model = desktop.getCurrentComponent()

        print("END: constructor")
然后我称之为:

myUno = UnoStruct()
BEGIN: constructor
END: constructor
并试图得到它

active_sheet = myUno.model.CurrentController.ActiveSheet

AttributeError: 'NoneType' object has no attribute 'CurrentController'
而且
模型
似乎是
(空)

>active\u sheet=myUno.model
>>>打印(myUno.model)
没有一个
>>>打印(myUno)
那么它在构造函数中发生了什么?它不应该还在那里吗?我试图避免使用锅炉板代码。

您需要:

\uuu init\uuu
中的
模型
变量是该方法的本地变量,除非您指定它,否则不会附加到实例
self

当您在类的正下方定义了
model
,但在
\uuuu init\uuuu
方法之外,您定义了一个,它将出现在该类的所有实例中

否则,当您访问
myUno.model
时,您将面临一个
AttributeError

您需要:

\uuu init\uuu
中的
模型
变量是该方法的本地变量,除非您指定它,否则不会附加到实例
self

当您在类的正下方定义了
model
,但在
\uuuu init\uuuu
方法之外,您定义了一个,它将出现在该类的所有实例中


如果没有这一点,当您访问
myUno.model
时,您将面临一个
AttributeError

我将添加到Barros的答案中,您将
localContext=None、resolver=None等声明为类变量。因此,修改后的代码如下(如果您需要所有这些变量作为实例变量):


我想在Barros的答案中添加一点,您将
localContext=None、resolver=None等声明为类变量。因此,修改后的代码如下(如果您需要所有这些变量作为实例变量):


另外,我知道它实际上不是一个结构,我只是这样称呼它。你从来没有设置过
myUno.model
,所以从类定义来看它仍然是
None
。来自的
UnoObjs
类就是这样做的。我知道它实际上不是一个结构,我只是这样称呼它。你从来没有设置过
myUno.model
,所以从类定义来看它仍然是
None
。来自的
unobjs
类就是这样做的。所以
self
就像Java中的
this
。@leeand00是的。区别在于,
self
不是一个关键字(只是一个常规名称):您可以用
obj
这个
,不管什么来代替它,因为参数是显式的(这对于最初没有设计为面向对象的语言来说非常常见)。对,因为它是一个参数,所以您可以自己命名它。但是如果你想让另一个Python程序员轻松阅读它,最好坚持
self
惯例。因此
self
就像Java中的
这个
,那么@leeand00是的。区别在于,
self
不是一个关键字(只是一个常规名称):您可以用
obj
这个
,不管什么来代替它,因为参数是显式的(这对于最初没有设计为面向对象的语言来说非常常见)。对,因为它是一个参数,所以您可以自己命名它。但是,如果您想让另一位Python程序员轻松阅读,最好坚持
self
惯例。
>>> active_sheet = myUno.model
>>> print( myUno.model )
None
>>> print( myUno )
<__main__.UnoStruct object at 0x7faea8e06748>
   self.model = desktop.getCurrentComponent()
class UnoStruct():
    def __init__(self ):
        # get the uno component context from the PyUNO runtime
        self.localContext = uno.getComponentContext()

        # create the UnoUrlResolver
        self.resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )

        # connect to the running office
        self.ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        self.smgr = ctx.ServiceManager

        # get the central desktop object
        self.desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

        # access the current writer document
        self.model = desktop.getCurrentComponent()