Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable Types in a class
#9
If you are actively using a type checker the following code would flag an error
from dataclasses import dataclass
from typing import Optional


@dataclass
class Node:
    value: float
    next: Optional["Node"] = None
    prev: Optional["Node"] = None


node = Node(0.1)
node.string = "Hello"
For instance in vscode with the type checker mode set to at least basic, pylance would put a red line under string and hovering over it would show the following:
Error:
string: Literal['Hello'] Cannot assign member "string" for type "Node" Member "string" is unknown

Python version 3.10 added slots as a parameter to dataclass, The following can now be done
from dataclasses import dataclass
from typing import Optional


@dataclass(slots=True)
class Node:
    value: float
    next: Optional["Node"] = None
    prev: Optional["Node"] = None


node = Node(0.1)
node.string = "Hello"
this will give the same AttributeError: 'Node' object has no attribute 'string' error
Reply


Messages In This Thread
Variable Types in a class - by nafshar - Oct-06-2022, 07:20 PM
RE: Variable Types in a class - by XavierPlatinum - Oct-06-2022, 07:31 PM
RE: Variable Types in a class - by nafshar - Oct-06-2022, 07:48 PM
RE: Variable Types in a class - by deanhystad - Oct-06-2022, 08:31 PM
RE: Variable Types in a class - by ndc85430 - Oct-06-2022, 08:33 PM
RE: Variable Types in a class - by Yoriz - Oct-06-2022, 09:21 PM
RE: Variable Types in a class - by deanhystad - Oct-07-2022, 03:39 AM
RE: Variable Types in a class - by nafshar - Oct-07-2022, 10:26 AM
RE: Variable Types in a class - by Yoriz - Oct-07-2022, 02:57 PM
RE: Variable Types in a class - by deanhystad - Oct-07-2022, 07:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,098 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Distinguishing different types of class attributes Drone4four 4 2,172 Feb-21-2022, 06:34 PM
Last Post: deanhystad
  Calling a base class variable from an inherited class CompleteNewb 3 1,775 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,077 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Class variable / instance variable ifigazsi 9 4,446 Jul-28-2020, 11:40 AM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,370 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  Limiting valid values for a variable in a class WJSwan 5 3,771 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  Newbie: Help with variable selection in a class Slack86 5 2,789 Apr-07-2020, 08:41 PM
Last Post: jefsummers
  Using variable from a class in another .py script keegan_010 4 3,109 Mar-25-2020, 07:47 AM
Last Post: keegan_010
  How to access class variable? instances vs class drSlump 5 3,447 Dec-11-2019, 06:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020