Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) 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.