User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,228 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 2,202 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: 246 | Replies: 4
Reply
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Cloning variables?

  #1  
Jul 22nd, 2008
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: Cloning variables?

  #2  
Jul 22nd, 2008
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. **
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Re: Cloning variables?

  #3  
Jul 22nd, 2008
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:
  1. >>> class item:
  2. name = "An item"
  3. creationName = something
  4.  
  5.  
  6. >>> itemInstance = item()
  7. >>> itemInstance.name
  8. 'An item'
  9. >>> itemInstance.creationName
  10. 'An item'
  11. >>> itemInstance.name = "Different name."
  12. >>> itemInstance.name
  13. 'Different name.'
  14. >>> itemInstance.creationName
  15. 'Different name.'

Can this be done?
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: Cloning variables?

  #4  
Jul 23rd, 2008
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:
  1. >>> class item:
  2. ... def __init__( self, name = None ):
  3. ... self.name = name
  4. ... self.creationName = name
  5. ... def changeName( self, new_name ):
  6. ... self.name = new_name
  7. ... self.creationName = new_name
  8. ...
  9. >>> itemInst = item()
  10. >>> itemInst.changeName( 'An item' )
  11. >>> itemInst.name
  12. 'An item'
  13. >>> itemInst.creationName
  14. 'An item'
  15. >>> itemInst.changeName( 'Different name' )
  16. >>> itemInst.name
  17. 'Different name'
  18. >>> itemInst.creationName
  19. 'Different name'
  20. >>>
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. **
Reply With Quote  
Join Date: Jul 2008
Posts: 194
Reputation: Gribouillis is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 29
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Junior Poster

Re: Cloning variables?

  #5  
Jul 31st, 2008
There is a solution using properties, that is, dynamic attributes. Here is the code

  1. class Item(object):
  2. def __init__(self, name = ""):
  3. self._name = name
  4. self._cname = None
  5.  
  6. def _get_name(self):
  7. return self._name
  8. def _set_name(self, value):
  9. self._name = value
  10. self._cname = None
  11. name = property(_get_name, _set_name)
  12.  
  13. def _get_creationName(self):
  14. return self.name if self._cname is None else self._cname
  15. def _set_creationName(self, value):
  16. self._cname = value
  17. creationName = property(_get_creationName, _set_creationName)
  18.  
  19. def showNames(self):
  20. return "name = % s, creationName = %s" % (self.name, self.creationName)
  21.  
  22. def testNames():
  23. item = Item("toto")
  24. print item.showNames()
  25.  
  26. item.name = "zorglub"
  27. print item.showNames()
  28.  
  29. item.creationName = "bazar"
  30. print item.showNames()
  31.  
  32. item.name = "floppy"
  33. print item.showNames()
  34.  
  35. if __name__ == "__main__":
  36. testNames()
  37.  

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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 11:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC