Saving the name of a instance of a class in a string variable
I have this strange question that im stuck for days. I'm developing a small program using python for maya. (By now I hope you know I'm not a professional programmer. just a side coder to build some internal scripts and tools)
The tool I am building has a lot of UI elements I am creating as classes, which have their own methods.
What I need to do, is to pass an instance of such a class, using a string. I know this is strange and pls let me explain.
I need to implement a drag and drop operation of a widget class, which can pass a message from the source to the destination. the message always gets forcefully (by Maya) gets converted into a string!!!
Class a(object):
def __init__(self, v)
self.v=v
instance=a("important")
normally what I can do is
print (instance.v)
but what happens is during the message exchange process, the instance gets converted to text and I get a string instead of the python object
'<main.a object at 0x000001BB0696CA90>'
which is a string,. and i can't run or query any methods in the actual function..
I know this is very noob. Please advice on how I can convert a class instance saved in a string to a python object so I can call up its methods or query variables..
Thanks again.
python maya
|
show 1 more comment
I have this strange question that im stuck for days. I'm developing a small program using python for maya. (By now I hope you know I'm not a professional programmer. just a side coder to build some internal scripts and tools)
The tool I am building has a lot of UI elements I am creating as classes, which have their own methods.
What I need to do, is to pass an instance of such a class, using a string. I know this is strange and pls let me explain.
I need to implement a drag and drop operation of a widget class, which can pass a message from the source to the destination. the message always gets forcefully (by Maya) gets converted into a string!!!
Class a(object):
def __init__(self, v)
self.v=v
instance=a("important")
normally what I can do is
print (instance.v)
but what happens is during the message exchange process, the instance gets converted to text and I get a string instead of the python object
'<main.a object at 0x000001BB0696CA90>'
which is a string,. and i can't run or query any methods in the actual function..
I know this is very noob. Please advice on how I can convert a class instance saved in a string to a python object so I can call up its methods or query variables..
Thanks again.
python maya
1
what happens if you writeself.value = v
– YOLO
Dec 28 '18 at 19:36
yes. my mistake on the question. fixed it. but still the problem still remains!
– Rasika Jayawardene
Dec 28 '18 at 19:43
1
Did you try to serialize and de-serialize the instance of class using Pickle?
– Kiran Baktha
Dec 28 '18 at 19:46
what is the expected output ?
– YOLO
Dec 28 '18 at 19:46
i want a way to covert a string which has the name and memory location of the instance into a variable i can actually call on the methods!! From '<__main__.test_class object at 0x000001BB0696CA90>' to a new_instance.v
– Rasika Jayawardene
Dec 28 '18 at 20:20
|
show 1 more comment
I have this strange question that im stuck for days. I'm developing a small program using python for maya. (By now I hope you know I'm not a professional programmer. just a side coder to build some internal scripts and tools)
The tool I am building has a lot of UI elements I am creating as classes, which have their own methods.
What I need to do, is to pass an instance of such a class, using a string. I know this is strange and pls let me explain.
I need to implement a drag and drop operation of a widget class, which can pass a message from the source to the destination. the message always gets forcefully (by Maya) gets converted into a string!!!
Class a(object):
def __init__(self, v)
self.v=v
instance=a("important")
normally what I can do is
print (instance.v)
but what happens is during the message exchange process, the instance gets converted to text and I get a string instead of the python object
'<main.a object at 0x000001BB0696CA90>'
which is a string,. and i can't run or query any methods in the actual function..
I know this is very noob. Please advice on how I can convert a class instance saved in a string to a python object so I can call up its methods or query variables..
Thanks again.
python maya
I have this strange question that im stuck for days. I'm developing a small program using python for maya. (By now I hope you know I'm not a professional programmer. just a side coder to build some internal scripts and tools)
The tool I am building has a lot of UI elements I am creating as classes, which have their own methods.
What I need to do, is to pass an instance of such a class, using a string. I know this is strange and pls let me explain.
I need to implement a drag and drop operation of a widget class, which can pass a message from the source to the destination. the message always gets forcefully (by Maya) gets converted into a string!!!
Class a(object):
def __init__(self, v)
self.v=v
instance=a("important")
normally what I can do is
print (instance.v)
but what happens is during the message exchange process, the instance gets converted to text and I get a string instead of the python object
'<main.a object at 0x000001BB0696CA90>'
which is a string,. and i can't run or query any methods in the actual function..
I know this is very noob. Please advice on how I can convert a class instance saved in a string to a python object so I can call up its methods or query variables..
Thanks again.
python maya
python maya
edited Dec 28 '18 at 19:47
Rasika Jayawardene
asked Dec 28 '18 at 19:27
Rasika JayawardeneRasika Jayawardene
93
93
1
what happens if you writeself.value = v
– YOLO
Dec 28 '18 at 19:36
yes. my mistake on the question. fixed it. but still the problem still remains!
– Rasika Jayawardene
Dec 28 '18 at 19:43
1
Did you try to serialize and de-serialize the instance of class using Pickle?
– Kiran Baktha
Dec 28 '18 at 19:46
what is the expected output ?
– YOLO
Dec 28 '18 at 19:46
i want a way to covert a string which has the name and memory location of the instance into a variable i can actually call on the methods!! From '<__main__.test_class object at 0x000001BB0696CA90>' to a new_instance.v
– Rasika Jayawardene
Dec 28 '18 at 20:20
|
show 1 more comment
1
what happens if you writeself.value = v
– YOLO
Dec 28 '18 at 19:36
yes. my mistake on the question. fixed it. but still the problem still remains!
– Rasika Jayawardene
Dec 28 '18 at 19:43
1
Did you try to serialize and de-serialize the instance of class using Pickle?
– Kiran Baktha
Dec 28 '18 at 19:46
what is the expected output ?
– YOLO
Dec 28 '18 at 19:46
i want a way to covert a string which has the name and memory location of the instance into a variable i can actually call on the methods!! From '<__main__.test_class object at 0x000001BB0696CA90>' to a new_instance.v
– Rasika Jayawardene
Dec 28 '18 at 20:20
1
1
what happens if you write
self.value = v– YOLO
Dec 28 '18 at 19:36
what happens if you write
self.value = v– YOLO
Dec 28 '18 at 19:36
yes. my mistake on the question. fixed it. but still the problem still remains!
– Rasika Jayawardene
Dec 28 '18 at 19:43
yes. my mistake on the question. fixed it. but still the problem still remains!
– Rasika Jayawardene
Dec 28 '18 at 19:43
1
1
Did you try to serialize and de-serialize the instance of class using Pickle?
– Kiran Baktha
Dec 28 '18 at 19:46
Did you try to serialize and de-serialize the instance of class using Pickle?
– Kiran Baktha
Dec 28 '18 at 19:46
what is the expected output ?
– YOLO
Dec 28 '18 at 19:46
what is the expected output ?
– YOLO
Dec 28 '18 at 19:46
i want a way to covert a string which has the name and memory location of the instance into a variable i can actually call on the methods!! From '<__main__.test_class object at 0x000001BB0696CA90>' to a new_instance.v
– Rasika Jayawardene
Dec 28 '18 at 20:20
i want a way to covert a string which has the name and memory location of the instance into a variable i can actually call on the methods!! From '<__main__.test_class object at 0x000001BB0696CA90>' to a new_instance.v
– Rasika Jayawardene
Dec 28 '18 at 20:20
|
show 1 more comment
2 Answers
2
active
oldest
votes
How about writing a method for your class which returns the string?
class A(object):
def __init__(self, v):
self.v = v
def getV(self):
return self.v
Result:
>>> Instance = A('important')
>>> print(Instance.getV())
important
>>> Instance.getV()
'important'
If that doesn't work maybe you could write a printV() method...
add a comment |
Well.. pickeling worked!! i just wanted a way of creating a string from an instance and using the string to make the reference to the instance again.!! thanks, @kiran Baktha!
i just did
beforetransfer = pickel.dumps(instance)
and at the recieving end
aftertransfer = pickel.loads(beforetransfer)
thanks community!! you guys are amazzing!!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963416%2fsaving-the-name-of-a-instance-of-a-class-in-a-string-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about writing a method for your class which returns the string?
class A(object):
def __init__(self, v):
self.v = v
def getV(self):
return self.v
Result:
>>> Instance = A('important')
>>> print(Instance.getV())
important
>>> Instance.getV()
'important'
If that doesn't work maybe you could write a printV() method...
add a comment |
How about writing a method for your class which returns the string?
class A(object):
def __init__(self, v):
self.v = v
def getV(self):
return self.v
Result:
>>> Instance = A('important')
>>> print(Instance.getV())
important
>>> Instance.getV()
'important'
If that doesn't work maybe you could write a printV() method...
add a comment |
How about writing a method for your class which returns the string?
class A(object):
def __init__(self, v):
self.v = v
def getV(self):
return self.v
Result:
>>> Instance = A('important')
>>> print(Instance.getV())
important
>>> Instance.getV()
'important'
If that doesn't work maybe you could write a printV() method...
How about writing a method for your class which returns the string?
class A(object):
def __init__(self, v):
self.v = v
def getV(self):
return self.v
Result:
>>> Instance = A('important')
>>> print(Instance.getV())
important
>>> Instance.getV()
'important'
If that doesn't work maybe you could write a printV() method...
answered Dec 28 '18 at 20:32
jorijnsmitjorijnsmit
589421
589421
add a comment |
add a comment |
Well.. pickeling worked!! i just wanted a way of creating a string from an instance and using the string to make the reference to the instance again.!! thanks, @kiran Baktha!
i just did
beforetransfer = pickel.dumps(instance)
and at the recieving end
aftertransfer = pickel.loads(beforetransfer)
thanks community!! you guys are amazzing!!
add a comment |
Well.. pickeling worked!! i just wanted a way of creating a string from an instance and using the string to make the reference to the instance again.!! thanks, @kiran Baktha!
i just did
beforetransfer = pickel.dumps(instance)
and at the recieving end
aftertransfer = pickel.loads(beforetransfer)
thanks community!! you guys are amazzing!!
add a comment |
Well.. pickeling worked!! i just wanted a way of creating a string from an instance and using the string to make the reference to the instance again.!! thanks, @kiran Baktha!
i just did
beforetransfer = pickel.dumps(instance)
and at the recieving end
aftertransfer = pickel.loads(beforetransfer)
thanks community!! you guys are amazzing!!
Well.. pickeling worked!! i just wanted a way of creating a string from an instance and using the string to make the reference to the instance again.!! thanks, @kiran Baktha!
i just did
beforetransfer = pickel.dumps(instance)
and at the recieving end
aftertransfer = pickel.loads(beforetransfer)
thanks community!! you guys are amazzing!!
answered Dec 28 '18 at 20:45
Rasika JayawardeneRasika Jayawardene
93
93
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963416%2fsaving-the-name-of-a-instance-of-a-class-in-a-string-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
what happens if you write
self.value = v– YOLO
Dec 28 '18 at 19:36
yes. my mistake on the question. fixed it. but still the problem still remains!
– Rasika Jayawardene
Dec 28 '18 at 19:43
1
Did you try to serialize and de-serialize the instance of class using Pickle?
– Kiran Baktha
Dec 28 '18 at 19:46
what is the expected output ?
– YOLO
Dec 28 '18 at 19:46
i want a way to covert a string which has the name and memory location of the instance into a variable i can actually call on the methods!! From '<__main__.test_class object at 0x000001BB0696CA90>' to a new_instance.v
– Rasika Jayawardene
Dec 28 '18 at 20:20