LoginSignup
0
0

【Javascript、Python】変数名で使える文字って結局何?

Posted at

導入

最近の言語、意外と変数名にいろんな文字使えますよね?
結局何文字くらい使えるのか気になったのでまとめてみました。
javascriptはWindows11のFirefoxとEdge(結果は同じ)
pythonは動作環境なんて持っていないので、google colabです。

使える文字

pythonの方は若干ミスってますが(詳細は下)
予想は、Lが1文字目から使えて、Mが2文字目から使える感じかなと思っています。
記号系は_とか$とかを除き基本的に使えないのかなと思っています。

Javascript

const character_usable_in_variable_name = new Map();
for(let $i = 1; $i<205745; $i++){
  try{
    eval(`${String.fromCodePoint($i)}=0`);
    character_usable_in_variable_name.set($i, String.fromCodePoint($i));
  }catch(e){
    
  }
}
console.log(character_usable_in_variable_name);
let str = '';
for($j of character_usable_in_variable_name.keys()){
  str += character_usable_in_variable_name.get($j);
}
console.log(str);

Python

character_usable_in_variable_name = []
for int in range(1000000):
  try:
    exec(chr(int) + '=0')
    character_usable_in_variable_name.append([int, chr(int)])
  except:
    0
#print(character_usable_in_variable_name)

str = ''
for i, j in character_usable_in_variable_name:
  str += j
print(str)

ちなみになぜ#が入っているかというと、

#=0

でコメントアウトされ、エラーが一つも出ないからです。
その点javascriptではコメントアウトが二文字以上なのでこのようなことは起こらないですね。
あとコピーミスって改行が入っていますね。

JSとPYで使える文字の比較

const python_txt = [];
for($k of `#ABC...略`){python_txt.push($k)}

const javascript_txt = [];
for($j of character_usable_in_variable_name.keys()){
  target = character_usable_in_variable_name.get($j)
  point = python_txt.indexOf(target);
  if(point != -1) {
    python_txt.splice(point, 1);
  } else {
    javascript_txt.push(target);
  }
}

なぜsplitではなくfor-ofを使ったかというと、
splitを使ってしまうと後ろのほうにいる"1文字表すのに2文字使う勢"が分割されてしまうからです

`𲎯`.split('');
  //> Array [ "\ud888", "\udfaf" ]

Javascript

pythonは'$'使えないんですね(一文字目だけかも)

Python

なし

最後に

雰囲気的に、jsはカテゴリがLなものは全部使えそうですね。
Pythonはunicode15.1のアップデートができていないような感じがします。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0