简体   繁体   中英

Python calling class methods from external module

I need help please, I have searching but none of the answers I found is working for my problem

I need to set a variable in a class method in another module

(frist.py)

class car(object)
   @staticmethod
   def reg_number(self, regno):
       self.builder.get_object("label1").set_text(regno)

   def __init__(self):
       self.data = Data()

.
.
.
.
if __name__ == "__main__":
     app = car()
     gtk.main()

(second.py)

from first import car

def set_reg_no():
    cr = car()
    cr.reg_number('CVM107')

The call to cr.reg_number('CVM107') is raising an error. I have tried car.reg_number('CVM107'), first.car.reg_number('CVM107') among a lot of other combinations but I keep getting errors

Any help will be appreciated

Thanks

Piet

You should remove the self argument in the staticmethod: Like this:

>>> class car(object):
...     @staticmethod
...     def test(str):
...             print str
... 
>>> car.test('a')
a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM