0

We can create object in Python in both following ways:

>>> foolist = []
>>> footuple = ()
>>> foostring = ''
>>> type(footuple)
<type 'tuple'>
>>> type(foolist)
<type 'list'>
>>> type(foostring)
<type 'str'>
>>> foolist1 = list

Or

>>> foolist1 = list()
>>> footuple1 = tuple()
>>> foostring1 = str()
>>> type(foolist1)
<type 'list'>
>>> type(footuple1)
<type 'tuple'>
>>> type(foostring1)
<type 'str'>

Second way is pretty clear, we almost always use this to create new object. But I'm unable to understand the working of first way. Also can I create another class that behaves in similar manner(first way)?

5
  • 3
    You can't. This works that wy, because lists, tuples, sets, strings, dictionaries, are builtin types with "syntax support". Commented Jul 7, 2018 at 9:57
  • @WillemVanOnsem I've guessed that. Can it be created by modifying source code(in c++)? I've knowledge of c++ but not experieced. Commented Jul 7, 2018 at 10:00
  • yes, you could change the ipython (github.com/ipython/ipython) interpreter source code, and thus change the grammer. But then technically, the language it interprets is no longer Python. Commented Jul 7, 2018 at 10:01
  • @WillemVanOnsem is there a reason that you explicitly mentioned ipython and not python? Commented Jul 7, 2018 at 10:05
  • 1
    @user1767754: python is not an interpreter. If you write python as command line, the interpreter is probably CPython. Commented Jul 7, 2018 at 10:07

1 Answer 1

2

Second way is pretty clear, we almost always use this to create new object.

Once a programmer gets used to the syntax of lists, sets, dictionaries, tuples, strings, etc. Then the former is typically cleaner, since the number of characters is limited, and one can use it to immediately add elements to collections.

Also can I create another class that behaves in similar manner(first way)?

You can't. This is syntax specifically designed to work with builtin Python types. You can not - unlike some other languages - make your own "mini language". Other programming languages like for exmple Haskell allow to introduce aribtrary operators. Other languages like Prolog do not attach semantics to operators at all. In some languages, you can even introduce entire subgrammers.

In Python the language itself is quite fixed: the {} creates an (empty) dictionary, () the empty tuple, 'foo', "foo", """foo""", etc. all create strings, etc. So the interpreter has some grammar definitions that are constructed for this. You can not (easily) add extra constructs.

You could of course write some sort of interpreter (in Python) that takes as input for example a string, and thus converts this to objects of your own making. Or you could change the source code of the Python interpreter (for example CPython or IPython) and add extra grammar. But then you have technically speaking defined a new language (a superset of Python).

Sign up to request clarification or add additional context in comments.

2 Comments

I should've mentioned clearly. when I said 'Second way is pretty clear, we almost always use this to create new object.' I meant in-general object not in-built object like of list, string and tuple. Anyway thanks.
For completeness, it might be useful to highlight your point around writing your interpreter with a few example languages that make use of the ast module and compile to python bytecodes, like Hy: docs.hylang.org/en/stable/quickstart.html more exhaustive list here: github.com/vindarel/languages-that-compile-to-python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.