#πŸ”’ AI is leaning towards dataclasses. Would appreciate a little lesson

69 messages Β· Page 1 of 1 (latest)

marble yarrowBOT
#

@calm mortar

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.

calm mortar
#

I would appreciate at least a bit of information regarding to dataclasses if not actual coding guidance.

stuck idol
#

A dataclass is really mostly a convenience facility for providing a lot of the common boilerplate we make by hand.

class Foo:
    def __init(self, a, b):
        self.a = a
        self.b = b
    def __str__(self):
        return f'Foo(a={self.a},b={self.b})'

and so on. The @dataclass decorator provides default versions of these things (you can override any of them still).

The other thing is that it lets you define the attributes with type annotations:

from dataclasses import dataclass

@dataclass
class FOO:
    a : int
    b : str

and also to provide factories for their default values (if a plain default value isn't enough).

#

!doc dataclasses

marble yarrowBOT
runic tangle
calm mortar
#

!e

@dataclass
class User:
    name: str
    age: int

u = User("Alice", 30)

print(u)
marble yarrowBOT
calm mortar
#

!e

from dataclass import dataclass

@dataclass
class User:
    name: str
    age: int

u = User("Alice", 30)

print(u)
marble yarrowBOT
stuck idol
calm mortar
#

!e

from dataclasses import dataclass

@dataclass
class User:
    name: str
    age: int

u = User("Alice", 30)

print(u)
marble yarrowBOT
calm mortar
#

!e

from dataclasses import dataclass

@dataclass
class User(slots=True):
    name: str
    age: int

u = User("Alice", 30)

print(u)
marble yarrowBOT
calm mortar
#

!e

from dataclasses import dataclass

@dataclass(slots=True)
class User:
    name: str
    age: int

u = User("Alice", 30)

print(u)
marble yarrowBOT
calm mortar
#

would you be able to explain the difference in this?

#

i was just having a convo about it with the LLM

#

it means it doesnt return it as a dictionary

#

.

#

so it enforces declaration

#

so only the values i assigned when the object is created

#

are the values that can be utilised

#

but now thats caused even more confusion because i thought that is what frozen is for...

plain yoke
#

By default class instances store their data in a __dict__ dictionary. When you access attributes of the instances, it looks up the values from the dictionary.

With slots, the data is instead held in an array. This takes up less space, is faster to access, and prevents arbitrary members from being added.

calm mortar
#

is the array, an array of Slot=Value

#

or like an array of arrays

#

or simply every slot has its own array

plain yoke
#

When you enable slots, each instance attribute is an element of an array.

#

I'll link to the official high level documentation for general reference: https://docs.python.org/3/reference/datamodel.html#slots

Python documentation

Objects, values and types: Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. Even code is represented by objects. Ev...

calm mortar
#

almost like a preallocation of ram

#

0x01 = VariableX
0x02 = VariableY

#

when you use slots instead of, for key in dictionary if key = this

you just call 0x01

stuck idol
#

More like: imagine the attributes are not in a dict keyed by the attribute name, but in a list, where the first named attribute has position 0 and the second 1 and so on. The Python byte code knows to access that way in the list instead of by attribute name in a dict

calm mortar
#

ohh i see

stuck idol
#

The access is faster, your code is unchanged, but you can't use open ended arbitrary other attributes in that class

calm mortar
#

and this is memory efficient, because it involves preallcoation (high-level)
rather than dynamic or whatever

stuck idol
#

that also

#

though the memory for a dict vs list is usually well outstripped by the memory for whatever objects are on the attributes anyway

plain yoke
stuck idol
#

with slots the array is a fixed size, no?

plain yoke
#

Yes. You'll get runtime errors if you try to add a member that wasn't included in __slots__.

stuck idol
calm mortar
#

yeah, the AI is recommending I utilise it for my large amounts of data I will be handling with this

#

appreciate the insight though guys

plain yoke
#

I don't know of any downside to using them. Having a closed set of members is usually a good choice anyway.

stuck idol
calm mortar
#

yeah like one object per certificate

stuck idol
calm mortar
#

and per asn

plain yoke
#

Idk about "security", other than it can make some mistakes more obvious.

calm mortar
#

simply because of a more clear error handler?

#

clearer exception whatever

plain yoke
#
class MyClass:
    def __init__(self):
        self.my_attr = 1

inst = MyClass()
inst.my__attr = 2  # Ideally this should fail loudly, but happens silently
calm mortar
#

oh i see

plain yoke
#

That's an easy typo to make, and it not doing something obvious is not ideal. This is the kind of dumb thing that you pull your hair out over for a while because it superficially seems fine

#

*Typo'd that hard

#

!e

class MyClass:
    __slots__ = ['my_attr']

    def __init__(self):
        self.my_attr = 1

inst = MyClass()
inst.my__attr = 2
marble yarrowBOT
# plain yoke !e ```python class MyClass: __slots__ = ['my_attr'] def __init__(self):...

:x: Your 3.14 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 8, in <module>
003 |     inst.my__attr = 2
004 |     ^^^^^^^^^^^^^
005 | AttributeError: 'MyClass' object has no attribute 'my__attr' and no __dict__ for setting new attributes. Did you mean: 'my_attr'?
plain yoke
#

Oh good. I haven't actually tested that in years lmao.

#

And it even gives a suggestion. Isn't that nice.

calm mortar
#

is indeed

#

thank you kind sir

marble yarrowBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.