简体   繁体   English

如何从main访问python类方法以及如何在另一个python文件中传输和访问变量

[英]How to access python class method from main and transfering and accessing the variable in another python file

I have S8Test.py with testFirewallS8 class and some methods. 我有带有testFirewallS8类和一些方法的S8Test.py。 I want to access the method declared inside this class form the main method. 我想从主方法访问此类内部声明的方法。 and set the variable from that method another python file contains the same variable to modify. 并从该方法设置变量,另一个python文件包含要修改的相同变量。 How can I do this: 我怎样才能做到这一点:

    #! /usr/bin/env python    
    __author__ = 'Milson Munakami'    
    __revision__ = '0.0.2'      
    import json    
    import urllib    
    import httplib    
    from scapy.all import *

    import unittest
    import os, sys, socket, struct, select, time 
    from threading import Thread     

    import logging    
    import traceback  

    from mininet.net import Mininet    
    from mininet.node import OVSSwitch, OVSKernelSwitch, Controller, RemoteController    
    from mininet.log import setLogLevel, info    
    from mininet.cli import CLI 

    class TestFirewallS8( unittest.TestCase ):
        def setUp(self): 
            self.controllerIp="127.0.0.1"    
            self.switch = "00:00:00:00:00:00:00:01"    
            self.destinationIp = "10.0.0.1"    
            self.startTime_ = time.time()    
            self.failed = False    
            self.reportStatus_ = True   
            self.name_ = "Firewall"    
            self.log = logging.getLogger("unittest")   

            "Create an empty network and add nodes to it."    
            self.net = Mininet( controller=RemoteController )    
            #Want to move this method call from outside the setUp method because it need to be initiated only once for the whole test but 
            #it need to access the class variables and pass it to another python file i.e. Events.py to perform some task on the object i.e. self    
            #self.CreateNet()    

        def createNet(self):    
            print "Me"

            info( '*** Adding controller\n' )    
            self.net.addController( 'c0' , controller=RemoteController,ip= "127.0.0.1", port=6633)    
            info( '*** Adding hosts\n' )    
            h1 = self.net.addHost( 'h1', ip='10.0.0.1' )    
            h2 = self.net.addHost( 'h2', ip='10.0.0.2' )    
            h3 = self.net.addHost( 'h3', ip='10.0.0.3' )  
            info( '*** Adding switch\n' )    
            s1 = self.net.addSwitch( 's1' )      
            info( '*** Creating links\n' )    
            self.net.addLink( h1, s1 )    
            self.net.addLink( h2, s1 )    
            self.net.addLink( h3, s1 )    

        def setFinalcondition(self):    
            Precondition.SetFinalcondition(self)  
            info( '*** Stopping network' )    
            self.net.stop()

        def testCreateFlow(self):    
            Events.createFlow(self)

def suite():
        suite = unittest.TestSuite()
        suite.addTest(unittest.makeSuite(TestFirewallS8))
        return suite

    if __name__ == '__main__':    
        #How to get run the method of testFirewallS8 class and set the variable of it like self.net
        suiteFew = unittest.TestSuite(testCreateFlow)
        TestFirewallS8("createNet")

In another Events.py I have: 在另一个Events.py中,我有:

def createFlow(self):   
    info( '*** Starting network\n')
    self.net.start()

    info( '*** Testing network connecivity\n')  
    #self.net.pingAll()
    h1 = self.net.get('h1') 
    h3 = self.net.get('h3') 
    h1.cmd('ping -c1 %s' % h3.IP())

I'm not completely clear what you're asking, but if you have a class definition: 我不清楚您要问的是什么,但是如果您有一个类定义:

class MyClass(object):
    def __init__(self, name):
        self.name = name

    def big_name(self):
        print(self.name.upper())

After you instantiate it ( MyClass('Bob') ), assign it to a variable. 实例化它( MyClass('Bob') )之后,将其分配给变量。

bob = MyClass('Bob')

You can then execute methods of that class instance by simply accessing it's attributes. 然后,您只需访问其属性即可执行该类实例的方法。

bob.big_name # accesses the "big_name" attribute on "bob": a bound method
bob.big_name() # executes the bound method

Attributes can also be variables, and you can freely access and reassign them as well: 属性也可以是变量,您也可以自由访问和重新分配它们:

bob.name # accesses the "name" attribute on "bob": a string
bob.name = 'Robert' # reassigns the "name" attribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将变量信息从一个python文件传输到另一个 - transfering variable info from one python file to another 使用方法python将变量传输到另一个文件 - Transfering variable to another file using methods, python 从 python class 中的另一个方法访问在方法内部创建的变量 - accessing a variable created inside a method from another method in a python class 如何从另一个 python 文件访问 class 方法中的变量? - How do I access the variable inside a class method from another python file? 如何在Python中从一个类访问类方法和类变量到另一个类的实例方法? - How to access class method and class variable from a class to another class's instance method in Python? Python - 从主文件导入 class 函数/方法以在另一个文件中使用 - Python - Import class function/method from main to use in another file Python - 从另一个 class 访问局部变量 - Python - accessing a local variable from another class 从Python中的另一个程序访问类/方法 - Accessing a class/method from another program in Python 从另一个类-Python和Kivy获取访问方法内部的变量 - Get access a variable inside a method from another class - Python and Kivy 从python中的另一个文件访问函数变量 - Accessing a function variable from another file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM