Home > Software design >  How to get input from function with 'self' as variable
How to get input from function with 'self' as variable

Time:01-11

I want a function where you can enter a string and this string can be reused in the class. This is what I tried but I can't get the variable global to use it outside the function in the class

test.py

from Moduletest import Test


Test().set_string("Hello")

Moduletest.py

class Test():   

    def set_string(self, target_string):
        global string
        string = target_string
        print(string)
    
    zeta = string

CodePudding user response:

You do not need to use the global modifier here. What you need, is to reference to the object instance by using the self object like so:

class Test:
    zeta = None
    def __init__(self):
        self.string = None

    def set_string(self, target_string):
        self.string = target_string
        print(self.string)
        Test.zeta = self.string

t = Test()
t.set_string('abc')

CodePudding user response:

Making a variable global in python is usually strongly discouraged. I would suggest using an instance of your class and then assigning the string to an attribute of that class.

class Test:
    my_string: str

    def__init__(self, target_string):
        self.my_string = target_string
    
    def get_string(self):
        return self.my_string

Then you can access my_string like this

my_class = Test('Hello World')
the_string = my_class.get_string()
print(the_string) # Prints 'Hello World'
# Or like this 
print(my_class.my_string) # Prints 'Hello World' as well

When asking a question on here please be descriptive as to what you are trying to achieve. In this instance you should specify why you are trying to make the string global, so that others can possibly recommend alternatives, or have a better understanding of the question.

CodePudding user response:

To manipulate the static variable defined in the test class, you can review the application below. In this way, you can observe the differences between the data member and the static variable.

class Test:
    zeta = ""                                      # static variable definition
    
    @classmethod                                   # constructor method
    def __init__(self, string):
        self.string = string
    
    @classmethod                                   # member method
    def getString(self):
        return self.string
    
    @classmethod                                   # member method
    def setString(self, value):
        self.string = value
        
    @staticmethod                                  # static method
    def set_static_string(self, target):
        self.setString(target)                     # set data member
        Test.zeta = target                         # set static variable
        
firstTestObject = Test("test")                     # create object
Test.set_static_string(firstTestObject, "first")   # set static variable
print("1: ", Test.zeta)                            # get static variable
print("2: ", firstTestObject.zeta)                 # get static variable

secondTestObject = Test("test")                    # create object
Test.set_static_string(secondTestObject, "second") # set static variable
print("3: ", Test.zeta)                            # get static variable

# The same value is printed as static variables are common to every object.
print("4: ", firstTestObject.zeta)                      
print("5: ", secondTestObject.zeta)                     

This application produces the following output:

1:  first
2:  first
3:  second
4:  second
5:  second

CodePudding user response:

Self is the variable where you reference variables for functions inside your instantiated class.

The way to instantiate your class and pass an argument using the constructor is like this.

 `class Test:`

      `def __init__(self,arg1)`

          `self.var1 = arg1`

You instantiate the class Test like this my_instance=Test("my_string")

You access var1 via . like this. my_instance.var1

Read the manual here https://docs.python.org/3/tutorial/classes.html

CodePudding user response:

Maybe try to do this in Moduletest.py:

class Test:
    def __init__(self, target_string):
        self.string = target_string
    
    def getStr(self):
        return self.target_string

And then in the main.py:

from Moduletest import Test

x = Test("Hello")
print(x.getStr) #output: Hello
  •  Tags:  
  • Related