Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Respect the Built-in Names

HayaoSuzuki
September 08, 2017

Respect the Built-in Names

PyCon JP 2017 Day1 Lightning Talk

HayaoSuzuki

September 08, 2017
Tweet

More Decks by HayaoSuzuki

Other Decks in Technology

Transcript

  1. Who Am I? About Me Name Hayao Suzuki (@CardinalXaro) Blog

    http://xaro.hatenablog.jp/ Major Mathematics (Combinatorics, Number Theory) Work Python Programmer at iRidge, Inc. Reviewed Books Effective Python (O’Reilly Japan) ΞϧΰϦζϜΫΠοΫϦϑΝϨϯε ୈ 2 ൛ (O’Reilly Japan) ॳΊͯͷ PHP (O’Reilly Japan) Effective Debugging (O’Reilly Japan) ͢Β͢ΒΘ͔Δ Python ʢᠳӭࣾʣ Python ͱ JavaScript Ͱ͸͡ΊΔσʔλϏδϡΞϥΠθʔγϣϯ (O’Reilly Japan) 2 / 9
  2. Examples sum is a name of built-in function but... sum

    = 0 for i in range(10): sum = sum + i print(sum) sum ΁ͷ୅ೖ͸ѱ͍จ໌ʂʂ 4 / 9
  3. Examples id is a name of built-in function but... id

    = 12345 name = "Ξϧςϥ" print(f"Α͏ͦ͜{name}͞Μ (id={id})") id ΁ͷ୅ೖ͸ѱ͍จ໌ʂʂ 5 / 9
  4. Examples list is a name of sequence class but... list

    = [1, 2, 3, 4, 5] for i in range(len(list)): print(list[i]) list ΁ͷ୅ೖ͸ѱ͍จ໌ʂʂ 6 / 9
  5. Examples dict is a name of mapping class but... dict

    = {"spam": 0, "egg": 1, "bacon":2} for k, v in dict.items(): print("%s: %s" % (k, v)) dict ΁ͷ୅ೖ͸ѱ͍จ໌ʂʂ 7 / 9
  6. Examples json is a name of built-in module but... import

    json import requests response = requests.get("https://examples.com") if response.status_code == 200: json = response.json() print(json.keys()) json ΁ͷ୅ೖ͸ѱ͍จ໌ʂʂ 8 / 9
  7. Conclusion Respect the built-in names Abuse of names causes a

    trouble. If you provide objects with the same name as a built-in value, read builtins module’s documents. 9 / 9