Python Forum
What colon (:) in Python mean in this case?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What colon (:) in Python mean in this case?
#5
There is a mistake in the tutorial,they have copy from PEP636 but forget the @dataclass .
So there see it's like this.
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

def where_is(point):
    match point:
        case Point(x=0, y=0):
            print("Origin")
        case Point(x=0, y=y):
            print(f"Y={y}")
        case Point(x=x, y=0):
            print(f"X={x}")
        case Point():
            print("Somewhere else")
        case _:
            print("Not a point")
Then it work as it should.
>>> point = Point(0, 0)
>>> where_is(point)
Origin
>>> 
>>> point = Point(0, 9)
>>> where_is(point)
Y=9
>>> 
>>> point = Point(4, 0)
>>> where_is(point)
X=4
>>> 
>>> point = Point('hello', 'world')
>>> where_is(point)
Somewhere else
Yapwc likes this post
Reply


Messages In This Thread
RE: What colon (:) in Python mean in this case? - by snippsat - Dec-28-2022, 04:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove colon if value is None wardancer84 7 1,991 Feb-02-2022, 02:32 PM
Last Post: ibreeden
  Switch case or match case? Frankduc 9 4,637 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,701 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 1,911 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to use switch/case in python? newbieguy 9 4,172 Nov-08-2019, 11:35 AM
Last Post: newbieguy
  How to write switch case statement in Python pyhelp 9 9,347 Nov-11-2018, 08:53 PM
Last Post: woooee
  Is there a something like "case" in Python 3.6? Raptor88 18 21,041 Feb-27-2017, 02:44 AM
Last Post: Raptor88

Forum Jump:

User Panel Messages

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