class Dog: def __init__(name): super().__init__() self.Name = name SetDogsName(Dog dog, string newName) dog.Name = newName return dog # create a new dog object myDog = Dog("Sage") # call SetDogsName which returns a dog object referencedDog = SetDogsName(myDog, "Max") # note that even though SetDogsName returned a dog object, and we set that to a new variable, the object id in memory is the same print("myDog unique object Id: " + hash(myDog) + " referenceDog unique object Id: " + hash(referencedDog)) print("myDog name: " + myDog.Name + " is the same as referencedDog name: " + referencedDog.Name) # change referencedDog name as proof referencedDog.Name = "Sage" print("myDog name: " + myDog.Name + " is still the same as referencedDog name: " + referencedDog.Name) # now we are reassigning myDog to a brand new instance of the class myDog = Dog("Max") # now we can see that the object IDs are now differnt print("myDog unique object Id: " + hash(myDog) + " referenceDog unique object Id: " + hash(referencedDog))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question