iT邦幫忙

0

python data type 一問

  • 分享至 

  • xImage

大家好, 請問一下如果要在python 中表達java 的short 型態

  1. short a = value

  2. 判斷是否short data type

是應該怎麼寫, 但Pythonn 沒有short , 在網上說short 是16-bit signed two's complement integer. 真的不太清楚. 希望大家可以幫幫忙, 謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
一級屠豬士
iT邦大師 1 級 ‧ 2022-03-29 12:38:15

https://ithelp.ithome.com.tw/upload/images/20220329/20050647SG7ItVKVYt.png

看更多先前的回應...收起先前的回應...
diu7me iT邦新手 4 級 ‧ 2022-03-29 14:10:31 檢舉

謝謝你的幫忙, 屠豬士

diu7me iT邦新手 4 級 ‧ 2022-03-29 14:20:27 檢舉

不好意思, 如果是if checking 是不是ctypes.c_short是怎樣寫?

diu7me iT邦新手 4 級 ‧ 2022-03-29 14:33:31 檢舉

if isinstance(data, ctypes.c_short)

是這樣嗎?

Python 設計時,基本資料型態有int
https://docs.python.org/3/library/functions.html#int

不像一些語言會依照 C 的慣例有 short int.
Python提供了 ctypes 是為了與 C 做一些協同作業,所以一些檢查等等並不見得有提供.

至於你的問題,在於 Java 改寫為 Python ,並不是這樣一對一的語法對應.而是應該理解那段程式碼的意義.為何要用 short int ?
有什麼特別的用處嗎? 還是只是以前為了節省記憶體?
而 short int 有數值範圍是 -32768 ~ 32767, 若是要限制範圍,
是否該用一個函數來檢查,而不是檢查型態,因為Python 內建是沒有的.

diu7me iT邦新手 4 級 ‧ 2022-03-29 16:35:41 檢舉

我要發表回答

立即登入回答