•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 426,817 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,897 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 245 | Replies: 4
![]() |
•
•
Join Date: Apr 2008
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 0
I know, this is a stupid question, but is it possible for a class to have two variables, one of which has a value, and the other of which will always hold the same value, unless the user changes it?
In essence, making the second variable a duplicate of the first unless changed. Is this possible? And if so, how?
In essence, making the second variable a duplicate of the first unless changed. Is this possible? And if so, how?
What? Are you asking about two variables of the same name ? Or are you asking about a private variable that can only change through a public modifier?
Let's Go Pens!
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
•
•
Join Date: Apr 2008
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 0
Sorry, was I unclear? In essence, what I'm asking is how to construct a pointer in Python.
Suppose, for instance, you have a simple game, with an item creation mechanic that combines items into a new item. To add some mystery to it, you want the *option* of either showing what item will be produced or hiding it behind a "???" or something.
So, ideally, you would create an 'item' class, and give it two properties:
'name', and 'creationName' or something of the sort, where 'name' is the item's name, and 'creationName' is what the player is shown when creating it.
Now, then, you could simply set both of them to what you want whenever you made an item instance, but it would be much more convenient if 'creationName' automatically took on whatever value 'name' had, unless you set it to a different value. Here's more or less how it would look, ideally:
Can this be done?
Suppose, for instance, you have a simple game, with an item creation mechanic that combines items into a new item. To add some mystery to it, you want the *option* of either showing what item will be produced or hiding it behind a "???" or something.
So, ideally, you would create an 'item' class, and give it two properties:
'name', and 'creationName' or something of the sort, where 'name' is the item's name, and 'creationName' is what the player is shown when creating it.
Now, then, you could simply set both of them to what you want whenever you made an item instance, but it would be much more convenient if 'creationName' automatically took on whatever value 'name' had, unless you set it to a different value. Here's more or less how it would look, ideally:
Python Syntax (Toggle Plain Text)
>>> class item: name = "An item" creationName = something >>> itemInstance = item() >>> itemInstance.name 'An item' >>> itemInstance.creationName 'An item' >>> itemInstance.name = "Different name." >>> itemInstance.name 'Different name.' >>> itemInstance.creationName 'Different name.'
Can this be done?
It's not exactly possible to have an assignment triggered off of the modification of another object without actually monitoring it with a timer function or something that is running in the background but what if you used a function to set both of the names to the same thing like this:
python Syntax (Toggle Plain Text)
>>> class item: ... def __init__( self, name = None ): ... self.name = name ... self.creationName = name ... def changeName( self, new_name ): ... self.name = new_name ... self.creationName = new_name ... >>> itemInst = item() >>> itemInst.changeName( 'An item' ) >>> itemInst.name 'An item' >>> itemInst.creationName 'An item' >>> itemInst.changeName( 'Different name' ) >>> itemInst.name 'Different name' >>> itemInst.creationName 'Different name' >>>
Let's Go Pens!
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
There is a solution using properties, that is, dynamic attributes. Here is the code
The output of this program is
so values of creationName seem to follow assignments of name, but it remains possible to assign creationName individually
python Syntax (Toggle Plain Text)
class Item(object): def __init__(self, name = ""): self._name = name self._cname = None def _get_name(self): return self._name def _set_name(self, value): self._name = value self._cname = None name = property(_get_name, _set_name) def _get_creationName(self): return self.name if self._cname is None else self._cname def _set_creationName(self, value): self._cname = value creationName = property(_get_creationName, _set_creationName) def showNames(self): return "name = % s, creationName = %s" % (self.name, self.creationName) def testNames(): item = Item("toto") print item.showNames() item.name = "zorglub" print item.showNames() item.creationName = "bazar" print item.showNames() item.name = "floppy" print item.showNames() if __name__ == "__main__": testNames()
The output of this program is
name = toto, creationName = toto name = zorglub, creationName = zorglub name = zorglub, creationName = bazar name = floppy, creationName = floppy
so values of creationName seem to follow assignments of name, but it remains possible to assign creationName individually
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: newbie: how do I test a byte string?
- Next Thread: noob trying to close socket :)


Linear Mode