Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What Are Python Literals?
#9
(Sep-22-2020, 08:57 PM)Julie Wrote: Another question to help clarify things. When I hear immutable vs. mutable, I think of the list data type (mutable) vs. tuple data type (immutable). But, I also see list and dictionary data type included as literals...even though they are both mutable. Are these exceptions to the rule for literals?

Probably depends on exactly what definition you're using.

I might (loosely) call this a literal dict constructor, or a literal dict assignment:
d = {'a': 1}
But if you go through the official python documentation, it doesn't use that term. The documentation only uses "literal" for strings (and bytes) and numbers, all of which are immutable. So I'm not sure that nitpicking over the specifics helps much here. You know the difference between mutable and immutable types, and you know how to create both. Some guides might use the term beyond that used in the documentation.

Quote:Also, would you define a literal as the value as you see it? What exactly does that mean? Can you compare the differences between a literal and non-literal? What makes one a literal and the other not a literal?

In my mind I think of the literal as a specific assignment, where the compiler is parsing the source to create (or create a reference to) a specific object. Nothing has to be constructed at runtime. I don't know that such a thought is particularly helpful or necessary to understand how the code works, thought. Outside of your question making me think about this, I've never considered it much before.
Reply


Messages In This Thread
What Are Python Literals? - by Julie - Sep-22-2020, 05:36 PM
RE: What Are Python Literals? - by bowlofred - Sep-22-2020, 06:28 PM
RE: What Are Python Literals? - by metulburr - Sep-22-2020, 06:31 PM
RE: What Are Python Literals? - by buran - Sep-22-2020, 06:35 PM
RE: What Are Python Literals? - by Julie - Sep-22-2020, 07:46 PM
RE: What Are Python Literals? - by ndc85430 - Sep-22-2020, 07:55 PM
RE: What Are Python Literals? - by Julie - Sep-22-2020, 08:57 PM
RE: What Are Python Literals? - by bowlofred - Sep-22-2020, 11:58 PM
RE: What Are Python Literals? - by Julie - Sep-23-2020, 02:10 PM
RE: What Are Python Literals? - by deanhystad - Sep-22-2020, 11:51 PM
RE: What Are Python Literals? - by perfringo - Sep-23-2020, 04:49 PM
RE: What Are Python Literals? - by Gribouillis - Sep-23-2020, 05:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  replace in code vs literals Skaperen 3 2,504 Mar-30-2021, 12:37 AM
Last Post: Skaperen
  string literals in a list. Not what I expected. tycarac 3 2,736 Dec-28-2019, 05:31 AM
Last Post: tycarac
  parenthesis around a tuple of literals in a for Skaperen 2 2,220 Aug-25-2019, 03:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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