Python Forum
How to convert a string "<... object at POINTER>" to an object?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert a string "<... object at POINTER>" to an object?
#1
I want to convert a string "<... object at POINTER>" to an object.
Python version: 3.7.4

class MyClass:
    def Test(self):
        print("Hello!")

c = MyClass()
s = str(c)
print("The value of the string s is", s)
 
c0 = convert_str_to_object(s) # This is undefined but how to fix this?
c0.Test() # This should print "Hello!"
Output:
The value of the string s is <__main__.MyClass object at 0x0000019C9A340908> Hello! (This should appears)
I want to convert the string s back to an object, but how?

The reason:
I want to make a something like a global static variable, but I don't know how do it with the program that's using Python as a script, that's why I convert my object to string then I'm able to store it to the program because it only accepts string, and when I run the script again, I can reuse the object from the stored string. Now you see that it's something like a static object, but I don't know how to do that.
Reply
#2
(Aug-08-2020, 06:59 PM)mandaxyz Wrote: The reason:
I want to make a something like a global static variable, but I don't know how do it with the program that's using Python as a script, that's why I convert my object to string then I'm able to store it to the program because it only accepts string, and when I run the script again, I can reuse the object from the stored string. Now you see that it's something like a static object, but I don't know how to do that.

What? I'm not really understanding why you want to store the object as a string. Can't you use the pickle module?
Reply
#3
You want to serialize python objects, use the pickle module
>>> class MyClass:
...     def test(self):
...         print('Hello!')
... 
>>> obj = MyClass()
>>> import pickle
>>> s = pickle.dumps(obj)
>>> s
b'\x80\x03c__main__\nMyClass\nq\x00)\x81q\x01.'
>>> obj2 = pickle.loads(s)
>>> obj2.test()
Hello!
In this example pickle serializes a MyClass instance as a bytes object that can potentially be stored in a file and reused later.
Reply
#4
(Aug-08-2020, 07:17 PM)ndc85430 Wrote: What? I'm not really understanding why you want to store the object as a string. Can't you use the pickle module?

Thank you, but I cannot use the pickle module because there is an error message "TypeError: can't pickle PyCapsule objects". What I did is as below:
import pickle

class CLASS:
    ...
    def Func():
        p = pickle.dumps(self.drawHandler) # I'm trying to pickle a function handler.
        ...
        unpickle = pickle.loads(p)
I give up on this method, the next method is how to make the kind of global static object? I don't know if it's an off-topic if I ask it here, but I will Google it now.
Reply
#5
mandaxyz Wrote:I give up on this method
Serialization of live objects is not a easy topic, don't give up just because you don't find a solution that works instantly in all cases. It takes some perseverance to reach your goals.

mandaxyz Wrote:the next method is how to make the kind of global static object
It is not clear at all what you mean by that. You could perhaps explain what you need exactly, or why.
Reply
#6
@Gribouillis, it's impossible to solve the problem if we talk about python only, I even tried to use the keyword 'global' but it didn't work because the global variable is killed and it doesn't exist anymore when the python script execution is finished, and after I re-run again the python script inside the program, the variable is gone.

But fortunately, I could solve the problem, I didn't use the keyword 'global' but the program can store a variable inside itself and I use it as a global variable. I cannot use 'pickle', it's impossible to use it, but I found a solution to store the variable object directly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,278 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  type object 'man' has no attribute 'centerX' Tempo 7 699 Mar-07-2025, 03:47 AM
Last Post: deanhystad
  float object can't be interpreted as an integer t23 2 786 Jan-18-2025, 07:36 PM
Last Post: deanhystad
  Storing DictProxy object and modifying its nested value? haihal 3 926 Dec-20-2024, 04:58 AM
Last Post: deanhystad
  Extract args=, value that was passed to Multiprocessing.Proc object during runtime? haihal 1 598 Dec-08-2024, 07:04 AM
Last Post: Gribouillis
  what is solution to save and reload a object? eue 4 1,715 Nov-23-2024, 02:15 PM
Last Post: DeaD_EyE
  Trying to get JSON object in python and process it further Creepy 2 977 Oct-24-2024, 08:46 AM
Last Post: buran
  How to move an object over time rather than instantly? temlotresid6 3 1,525 Oct-23-2024, 11:20 AM
Last Post: temlotresid6
  Forward __getattr__ to another object. deanhystad 1 723 Aug-30-2024, 07:16 AM
Last Post: Gribouillis
  How can I upload a .gz file to the Swift Object Storage using Python swift client? Hanginium65 0 718 Jul-24-2024, 03:24 PM
Last Post: Hanginium65

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020