Simple things x=y
Had someone in class ask about this:
Update: 1-21-18 So, playing with the class I made... This does not solve his dilema. Any changes made to the class will affect the stored object, too. So time to keep looking. Python has to have a way to copy an existing object and giving it a new id? Hrmmmm.....
Found this:
https://docs.python.org/3/library/copy.html
>>> x = 5
>>> y = x
>>> y = 0
>>>x
>0
How do we get y to point to x, and not be assigned to equal x and visa versa?
When the = sign is used, the variables get stored in the same memory space, so kinda like python is making a spot and saying, all these things are going to be the same thing. so, in location 898884848 there is x and y.... any time you say x = True, all the things in 898884848 are set to True. Same with y.
y = 'puppies' all the things in 898884848 are set to 'puppies'.
Python class's. The niffty storage container. It's own little box to keep things in.
I came up with this, but I'm not sure it was the answer he was looking for.
Update: 1-21-18 played with it a bit to make sure it did what I thought.
import copy
class StoreThing(object):
def __init__(self, something):
self.obj = something
# or:
#self.obj = copy.copy(something)
def get_thing(self):
return self.obj
def update_thing(self, obj):
self.obj = obj
#self.obj = copy.copy(obj)
spam = ['eggs', 'butter']
breakfast = StoreThing(spam)
x = breakfast.get_thing()
print(x)
spam.append('pepper')
x = breakfast.get_thing()
print(x)
breakfast.obj.append('popsicle')
print(spam)
print(breakfast.obj)
With copy.copy(something) the copy will be unchanged by changes to spam....
but can be altered by changes to the class attribute. Sounds like from the documentation, the new breakfast object, made by copy is a new construct, so for something simple it's fine.... and probably don't need a class at all.
So:
spam = ['eggs', 'ham', 'milk']
breakfast = copy.copy(spam)
But I imagine if it was a more complex thing to copy and store, maybe I'd want a container like a class that I could do things with, like return, update, add, remove, juxtapose.... all that stuff.
Like if I could find that new gremlin this machine has produced and put it in a class, and make some poke() prod() disessmbowel() I mean..... tickle.... I won't hurt you....
Might be offline a bit, Going to reinstall a new system and try again to save this machine. I downloaded and tried to get Discord working for my class last week on this laptop... I posted a message on the general chat, Oh... thursday I think... but nothing appeared. It popped up today. Suddenly Discord is working. I have a feeling that may be part of my issue with the 502 error... Not sure. I couldn't get into my Digital Ocean server yesterday. Going to poke it today see if it stirs.
Off I go. Have a glorious Monday! Woot woot goes the trolley!
Update: 1-21-18 So, playing with the class I made... This does not solve his dilema. Any changes made to the class will affect the stored object, too. So time to keep looking. Python has to have a way to copy an existing object and giving it a new id? Hrmmmm.....
Found this:
https://docs.python.org/3/library/copy.html
>>> x = 5
>>> y = x
>>> y = 0
>>>x
>0
How do we get y to point to x, and not be assigned to equal x and visa versa?
When the = sign is used, the variables get stored in the same memory space, so kinda like python is making a spot and saying, all these things are going to be the same thing. so, in location 898884848 there is x and y.... any time you say x = True, all the things in 898884848 are set to True. Same with y.
y = 'puppies' all the things in 898884848 are set to 'puppies'.
Python class's. The niffty storage container. It's own little box to keep things in.
I came up with this, but I'm not sure it was the answer he was looking for.
Update: 1-21-18 played with it a bit to make sure it did what I thought.
import copy
class StoreThing(object):
def __init__(self, something):
self.obj = something
# or:
#self.obj = copy.copy(something)
def get_thing(self):
return self.obj
def update_thing(self, obj):
self.obj = obj
#self.obj = copy.copy(obj)
spam = ['eggs', 'butter']
breakfast = StoreThing(spam)
x = breakfast.get_thing()
print(x)
spam.append('pepper')
x = breakfast.get_thing()
print(x)
breakfast.obj.append('popsicle')
print(spam)
print(breakfast.obj)
With copy.copy(something) the copy will be unchanged by changes to spam....
but can be altered by changes to the class attribute. Sounds like from the documentation, the new breakfast object, made by copy is a new construct, so for something simple it's fine.... and probably don't need a class at all.
So:
spam = ['eggs', 'ham', 'milk']
breakfast = copy.copy(spam)
But I imagine if it was a more complex thing to copy and store, maybe I'd want a container like a class that I could do things with, like return, update, add, remove, juxtapose.... all that stuff.
Like if I could find that new gremlin this machine has produced and put it in a class, and make some poke() prod() disessmbowel() I mean..... tickle.... I won't hurt you....
Might be offline a bit, Going to reinstall a new system and try again to save this machine. I downloaded and tried to get Discord working for my class last week on this laptop... I posted a message on the general chat, Oh... thursday I think... but nothing appeared. It popped up today. Suddenly Discord is working. I have a feeling that may be part of my issue with the 502 error... Not sure. I couldn't get into my Digital Ocean server yesterday. Going to poke it today see if it stirs.
Off I go. Have a glorious Monday! Woot woot goes the trolley!
Comments
Post a Comment