A certainl part of an if() structure never gets executed.

Chris Angelico rosuav at gmail.com
Wed Jun 19 05:14:12 EDT 2013


On Wed, Jun 19, 2013 at 6:55 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Wed, 19 Jun 2013 18:21:40 +1000, Chris Angelico wrote:
>
>> You can't reference an object without
>> somewhere having either a name or a literal to start it off.
>
> True, but not necessarily a name bound to the object you are thinking of:
>
> some_function()
>
> gives you an object, but it's not a literal, and "some_function" is not
> the name of the object you end up with.

You start with the object identified by some_function, then you call
it. Same thing. Okay, so according to the Python grammar some of these
things I've been treating as operators aren't classified as them; but
there are still operations done to existing objects to derive other
objects:

> The ways to refer to something are more
> interesting:
>
> * you can refer to a thing directly by referring to it as a literal;
>
> * you can refer to a thing bound to a name by referring to the name;

The two I started with

> * you can refer to a thing in a namespace by referring to the namespace
> in some fashion, followed by a dot, followed by the name in that
> namespace,  e.g. some_object.attribute, __import__('math').pi;

Retrieving an attribute of an object, whether that object be
referenced by name or by function call.

> * you can refer to a thing in a sequence by referring to the sequence in
> some fashion, followed by an index number in square brackets, e.g. seq[3];

Ditto. These can call magic methods; as far as I'm concerned, they're
equivalent to operators. You can apply them to anything.

> * you can refer to a thing that is returned by a callable (function,
> method, type, etc.) by referring in some fashion to that callable object,
> followed by calling it, e.g. functions[9](arg) gives you a reference to
> some object which may not be any of `functions`, `9`, or `arg`.

And same again. You start with functions, 9, and arg, look up two of
them as names, traverse the series of operations, and get back a
result. Or maybe you throw an exception.

>>> class Foo:
	def __call__(self):
		print("Hello, world!")

		
>>> foo=Foo()
>>> foo()
Hello, world!

Is foo a function? Kinda. Sorta. We don't care. Is the function call
notation () an operator? Ditto - we don't care. It works like one.
There's little fundamental difference between:

>>> "asdf %d qwer"%5
'asdf 5 qwer'

and

>>> "asdf %d qwer"[5]
'%'

but one of them is called an operator and one's not. Would you say
that percent notation there is another way to reference an object? Is
it a different type of string literal? No. It's a string literal and
an operation done to it. Same with the subscripting, even though
that's not technically an operator. It's not a different way to get an
object. It's an operation on an object.

ChrisA



More information about the Python-list mailing list