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?

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?

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:

>>> 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:

>>> 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'
>>>

There is a solution using properties, that is, dynamic attributes. Here is the code

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.