#🔒 node class for linked lists

73 messages · Page 1 of 1 (latest)

vagrant plume
#

can someone what this does?

__slots__ = 'element', 'next'

the context is this is in a node class for a linked list. ive never seen this __slots__ thing

stark condorBOT
#

@vagrant plume

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slate cloak
#

hate to be redirecting somewhere but this post explains it very well

fallow hound
#

!e

class Foo:
    __slots__ = ['a', 'b']

    def __init__(self):
        self.c = 123

Foo()
stark condorBOT
fallow hound
#

I'm not allowed to make an attribute named c here because slots doesn't have it

#

!e

class Foo:
    __slots__ = ['a', 'b', 'c']

    def __init__(self):
        self.c = 123

Foo()
stark condorBOT
vagrant plume
#

does it have anything to do with memory efficiency?

fallow hound
vagrant plume
#

i have something in my notes about that but im not sure it pretains to this or something else

#

ah ok

#

i also have something in here about dict for instanciated objects

fallow hound
#

read the link above that bones linked

#

it covers all of this

vagrant plume
#

ok thanks

vagrant plume
#

do attributes in classes only store references?

#

is this the same for variables?

fallow hound
#

this is beyond what I know about the inner workings of python

vagrant plume
fallow hound
#

sure

vagrant plume
#

so i tried referencing a immutable and mutable object and i did not expect this to happen and im not sure why it did

#

can i do !e inside a python snipet?

#

!e

class MyClass:
    def __init__(self, value):
        self.attribute = value

obj1 = MyClass(10)
obj2 = obj1

obj2.attribute = 20

print(obj1.attribute)
print(obj2.attribute)
stark condorBOT
vagrant plume
#

mutable object:

#

!e

class MyClass:
    def __init__(self, value):
        self.attribute = value

obj1 = MyClass([1, 2, 3])
obj2 = obj1

obj1.attribute.append(4)

print(obj1.attribute)
print(obj2.attribute)
fallow hound
stark condorBOT
vagrant plume
#

so when the object is mutable, changing obj1 also changes obj2

#

but when its immutable it doesnt?

fallow hound
#

obj1 and obj2 are always mutable here

vagrant plume
#

does that mean theres a new obj created?

fallow hound
#

the attribute being mutable is irrelevant

#
obj1 = MyClass(10)
obj2 = obj1
#

you now have 2 variables referencing the same object

#

this will never make a copy of the object

vagrant plume
#
obj1 = MyClass([1, 2, 3])
obj2 = obj1

obj1.attribute.append(4)

yeah but here the objs have different attribute even though they're referencing the same object?

#

wait sorry

#

this one has the same attribute

vagrant plume
fallow hound
#

it doesn't matter, the class instance is already the same object

#

obj1 and obj2 are the same object

#

so it doesn't matter which variable you use to access the object to modify it

#

it's the same object

vagrant plume
#

oh wait it doesnt wtf

#

sorry

#

i confused myself

fallow hound
#

mutable attribute is not relevant here

vagrant plume
#

yeah my bad i thought i saw a different output

#

so mutability has nothing to do with how objects are referenced?

fallow hound
#

that's not what I said

#

I said in your example, the mutable attribute not irrelevant because you were doubling up on the instance of MyClass

vagrant plume
#

yeah sorry i get that im just asking a follow up question

fallow hound
#
x = 'abc'
y = 'abc'
#

even though I'm creating two string literals, they are the same object

#

!e

x = 'abc'
y = 'abc'

print(id(x))
print(id(y))
stark condorBOT
fallow hound
#

!e

x = []
y = []

print(id(x))
print(id(y))
stark condorBOT
fallow hound
#

this would not happen if they were mutable

vagrant plume
#

oh thats so weird

fallow hound
#

why is it weird? Unique mutable objects need their own memory

#

but there's no advantage to immutable objects having unique memory

vagrant plume
#

thats true yeah

#

so the only bearing this has with attributes in a class is that if an attribute is a reference to a mutable object changing it is just a matter or changing the contents of the object where as changing an immutable object that a class attribute is referencing is a matter changing the reference to a completely new object

fallow hound
stark condorBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.