Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Let's Build a Timed Quiz App Simple Time Machine

Y B
Y B
14,136 Points

Got it right but have a quick question

I got the objective right with the below, but I'm confused about a general property of python. I'm not sure how the delorean function can access the starter variable without it being in an argument to the function, so wouldn't I need two arguments (int, starter)?

time_machine.py
import datetime

starter = datetime.datetime(2015, 10, 21, 16, 29)


def delorean(int1):
    timedif = datetime.timedelta(hours = int1)
    return starter + timedif

3 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Y B,

the starter variable in this case can be accessed by all the functions that you write as its scope is global.

It is not nested inside another function, it is (let's say) at the top level. This means that this variable can be accessed everywhere else as it is, with no need to specify it in the function's arguments.

It is not just related to python, it is something that happens to all the programming world as far as I understood.

Have a good day

Vittorio

Y B
Y B
14,136 Points

Ok thanks I was getting confused after going through classes and having a class variable (is that the right name) which is accessed within a method via using 'self' (i.e. that's how it is passed in for use in the method). Can a method inside a class access global variables (assuming they are in the same script)?

I haven't done the functional programming course yet... but heard that functional programme shouldn't have state changes - not entirely sure what that means but i.e. it shouldn't change global variables? Is that correct

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Variables that belong to self are instance variables. Variables at a higher scope can usually be accessed.

And, yeah, functional programming has, as a core belief, that you shouldn't mutate any variables in place. No side effects!

Y B
Y B
14,136 Points

Thanks, ok so effectively act as if every variable is immutable - or only affect variables as if they were immutable.

anyway - I'll wait until I do the course to find out more.

Thanks.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, pretty much. If you have a Pro membership, I did a Python functional programming workshop last month that's available there.