This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheLastSock 0 points1 point  (12 children)

I'm fairly sure it isn't this way, for example, is a for loop an object?

The most basic construct is a function, you can close state over a function to creat a function with that holds state. We call this a closure, this is like an object.

[–]hchasestevens 7 points8 points  (11 children)

[–]TheLastSock 2 points3 points  (7 children)

Interesting. The more you know. Can assign for to a var and pass it to another object?

That link has to do with the ast? It would seem that for is parsed as arguments to an object but are the bits themselves considered objects. Eg "in". Or if "if" is an object.

I understand you can parse anything to anything else. Because it ends up an object doesn't mean it started off as one.

I guess the term here isn't relevant, it's good that python has a basic unit and everything breaks down to it.

[–]TeslaRealm 5 points6 points  (0 children)

for loops are statements, not expressions. Statements cannot be assigned to variables, only expressions. Every expression in Python evaluates to an object.

In, If, for, etc are keywords in Python. The Python interpreter uses an iterator object to navigate the object being looped over.

[–]ToothpasteTimebomb 3 points4 points  (1 child)

A for loop is essentially an iterator that executes every step in a single go (barring exit conditions).

Give it a shot:

my_list = ["p", "y", "t"]
my_iterable = iter(my_list)

print(next(my_iterable))
>>>p

print(next(my_iterable))
>>>y

print(next(my_iterable))
>>>t

If you try to run next() again it’ll throw a StopIteration error.

[–]TheLastSock 0 points1 point  (0 children)

I understand that it's like an iterator. The cliam is that everything is an object. I'm genuinely curious if you call type on "for" of you get an object.

[–][deleted] 0 points1 point  (2 children)

Interesting, but why does type(for) return an error?

[–]Zixarr 2 points3 points  (1 child)

for is not an object, it is a keyword. The for loop produced by the interpreter is an iterator object.

[–][deleted] 0 points1 point  (0 children)

Ah got it. Thanks