Asked  7 Months ago    Answers:  5   Viewed   39 times

Which types of objects fall into the domain of "subscriptable"?



It basically means that the object implements the __getitem__() method. In other words, it describes objects that are "containers", meaning they contain other objects. This includes strings, lists, tuples, and dictionaries.

Tuesday, June 1, 2021
answered 7 Months ago

It is more or less historical: they used to be different a long time ago, which has no practical implications anymore.

Edit: I use "class" when referring to concrete implementations and "type" in a more informal way, when speaking about high level data structures, application arcitecture etc. In my thinking a type is a more general thing, I don't think of every class as a distinct type.

Also, when I use metaclasses (very rarely) I speak of types.

Wednesday, July 28, 2021
answered 5 Months ago

It simply means it can be serialized by the pickle module. For a basic explanation of this, see What can be pickled and unpickled?. The pickle protocol provides more details, and shows how classes can customize the process.

Sunday, August 1, 2021
answered 4 Months ago

The things to the left of the "=" are variables that get their value from the variable on the right.


script, first, second, third = argv

argv is a list of strings which in this case contains 4 items. These strings are "unpacked" and assigned to the four variables on the left of the =.

argv gets its value is when a Python program is invoked from the command line, like this: this is sure cool

in this case argv will contain ['', 'this', 'is', 'sure', 'cool']. These strings after the command are called "command line arguments" (see this tutorial) and the name of the script, and any arguments are stored in argv. This is a way to send information to the script when you start it.

In this case the variables get the following values:

  script is set to  ""  # the string is in argv[0]
  first to "is"     # argv[1]
  second to "sure"  # argv[2]


  third to "cool"   # argv[3]


  script, first, second, third = argv

is really equivalent to:

  script = argv[0]
  first = argv[1]
  second = argv[2]
  third = argv[3]

It's only that Python lets you do this assignment in one nice swoop.

Note that you can pull out your command line arguments in any order using the appropriate index value.

This mechanism is used to communicate information the to the Python script. You can imagine running a program that expects an input file and and output file. Instead of hardcoding them in your script, you could provide them on the command line. E.g., input.txt result.txt
Friday, August 6, 2021
answered 4 Months ago

Square brackets in VBA/VB6 are used for accessing "foreign identifiers", i.e. identifiers that would otherwise not be legal. For example:

Public Enum Foo
    [Some Thing] ' please don't do this
End Enum

And hidden members whose name begins with an underscore:

Public Property Get NewEnum() As IUnknown
    Set NewEnum = myCollection.[_NewEnum]
End Property

However in the context of the code in this question, the square brackets are, as Scott indicated, essentially shorthand notation for [_Global].Evaluate, which ultimately resolves to Application.Evaluate... assuming we're not in a Worksheet module's code-behind, in which case it's shorthand for Worksheet.Evaluate - and both return a Variant, which means any chained member calls are blind, late-bound calls resolved at run-time: Option Explicit can't save you from a typo.

That's why Rubberduck (an open-source VBIDE add-in project I manage / contribute to) resolves them as "runtime expressions":

Rubberduck's context-sensitive toolbar showing 'A1' as a 'runtime expression'

In other words this:

Range("E16").Value = Mid(line, 49, [6])

Could just as well be written like this:

[E16] = Mid(line, [49], [6])

...which is arguably terrible code with a ton of redundant implicit operations going on.

There is never, NEVER any reason whatsoever to square-bracket an integer literal: it's nothing more than a rather roundabout way to turn an Integer literal into a Double (since worksheet numeric values are Variant/Double):

Debug.Print TypeName(42)

Debug.Print TypeName([42])

Which is much better off done with an explicit conversion:

Debug.Print TypeName(CDbl(42))

...or even with a (gasp) type hint:

Debug.Print TypeName(42#)
Saturday, October 30, 2021
Hans-Peter Störr
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :