0

I am trying to write a function taking a string as an argument and using this argument as a class object.

Note that my explanantion might be strangely formulated sice I could not find an answer online. The MWE below should clarify what I mean, the problematic line is indicated.

Edit: in the MWE, "print" is an example. I need to be able to call the object to update it, print it or, in the case of a list, append to it. I need access to the object itself, not the value of the object.

MWE

# Create a class
class myClass():
    def __init__(self):
        self.one = "Test"
        self.two = "Plop"

# Define function
def myFunction (parameter):
    print(myObject.parameter)##### This line is currently not possible.

# Use class
myObject = myClass()

# Use function
myFunction("one")

I am not trying to append a new object to the class, only to call an existing object.

Is this even possible?

2 Answers 2

2

Looks like you need the built-in function called getattr

my_object = myClass()

def my_function(parameter):
    print(getattr(my_object, parameter, None))

also this is not the best practice to call objects from outer scope like that. i'd suggest to use dict magic methods:

class MyClass:
    def __init__(self):
        self.one = "Test"
        self.two = "Plop"

    def __getitem__(self, parameter):
        return getattr(self, parameter, None)

    def __setitem__(self, parameter, value):
        return setattr(self, parameter, value)

my_obj = MyClass()
parameter = "x"
print(my_obj[parameter])
my_obj[parameter] = "test"
print(my_obj.x)
Sign up to request clarification or add additional context in comments.

7 Comments

No, this is not what I want to do. It works for the "print" example, but I want to be able to manipulate the object itself, not just access its current value.
@AlMa you can do this too, via accessing self.__dict__. like this: my_object.__dict__["one"] = "Test1"
@AlMa but this is not the best thing to do. better ways is inheriting from dict so that you can do whatever you want like my_object[parameter]=1
Thank you, accessing self.__dict__ works perfecly. What do you mean by "inheriting from dict"? If I try my_object[parameter]=1 I get the error message "TypeError: 'myClass' object is not subscriptable"
@AlMa i checked and looks like inheriting from dict doesn't work as i expected. if you want to be able to use my_object[parameter]=1, you need to override the magic methods __setitem__ and __getitem__
|
0

You need to use getarttr():

# Create a class
class myClass():
    def __init__(self):
        self.one = "Test"
        self.two = "Plop"




# Use class
myObject = myClass()

# Define function
def myFunction(parameter):
    print(getattr(myObject, parameter))##### This line is currently possible.


# Use function
myFunction("one")

3 Comments

No, this is not what I want to do. It works for the "print" example, but I want to be able to manipulate the object itself, not just access its current value.
Can you give an example what you want to do? You post lacks clarity.
See the edit of my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.