Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • About Bonfire
Glyph
Glyph
@[email protected]  ·  activity timestamp 2 days ago

oof here's a cursed question:

how does one make a #python dataclass with a subclass that changes *only* the default values of its fields, without adding new ones? is that doable?

3
  • Copy link
  • Flag this post
  • Block
synlogic4242
synlogic4242
@[email protected] replied  ·  activity timestamp 2 days ago

@glyph yes. I remember doing it without annoted types as well. plain old Python classes and inheritance

  • Copy link
  • Flag this comment
  • Block
Glyph
Glyph
@[email protected] replied  ·  activity timestamp 2 days ago

oh you can just literally annotate it again, and it works, apparently

much easier than I thought

from dataclasses import dataclass

@dataclass
class A:
value: int = 0

@dataclass
class B(A):
value: int = 1

print(A())
print(B())

  • Copy link
  • Flag this comment
  • Block
Marsh Ray
Marsh Ray
@[email protected] replied  ·  activity timestamp 2 days ago

@glyph can it have a constructor that modifies them?
(sorry, my python is rusty)

1
  • Copy link
  • Flag this comment
  • Block
Glyph
Glyph
@[email protected] replied  ·  activity timestamp 2 days ago

@marshray doesn't need to, it seems! (see reply)

I had assumed this would be a type error of some kind, but no, it works…

  • Copy link
  • Flag this comment
  • Block

bonfire.mavnn.eu

News and community around mavnn.eu projects.

bonfire.mavnn.eu: About · Code of conduct · Privacy ·
Bonfire social · 1.0.1 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Public Groups
  • Code of Conduct