Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:37:30+00:00 2026-05-31T06:37:30+00:00

I am trying to set a new variable to reference a variable inside a

  • 0

I am trying to set a new variable to reference a variable inside a function. My pseudo code goes like this:

def myfunction():
   a bunch of stuff that results in
   myvariable = "Some Text"

Further down the code I have this:

for something in somethinglist:
   if something == iteminsomethinglist:
       myfunction()
       mynewvariable1 = myvariable
   elif something == iteminsomethinglist:
       myfunction()
       mynewvariable2 = myvariable
   else:
       mynewvariable3 = myvariable

I keep getting an error message that says something like: name ‘myvariable’ is not defined

I guess I thought that if I called the function, it processes some stuff, I pass result into a variable and then reference that variable to a more unique variable, it would store it….but it’s not.

Edit: I am attaching my code because it I wasn’t clear enough in my first post. There is a variable within my function I wanted to reference outside of it (actually there are 2) I apologize for not making it clear enough. I though my original psuedo code proposed the question well enough. I also have a feeling that this might not be the best approach. Possible calling 2 functions would be more appropriate? My code is below:

def datetypedetector():

    rows = arcpy.SearchCursor(fc)

    dateList = []

    for row in rows:
        dateList.append(row.getValue("DATE_OBSERVATION").strftime('%m-%d-%Y'))

    del row, rows

    newList = sorted(list(set(dateList)))

    dates = [datetime.strptime(d, "%m-%d-%Y") for d in newList]

    date_ints = set([d.toordinal() for d in dates])

    if len(date_ints) == 1:
        DTYPE = "Single Date"
        #print "Single Date"
    elif max(date_ints) - min(date_ints) == len(date_ints) - 1:
        DTYPE = "Range of Dates"
        #print "Range of Dates"
    else:
        DTYPE = "Multiple Dates"
        #print "Multiple Dates"

    fcList = arcpy.ListFeatureClasses()

for fc in fcList:

    if fc == "SO_SOIL_P" or fc == "AS_ECOSITE_P":
        datetypedetector()
        ssDate = newList
        print fc + " = " + str(ssDate)
        ssDatetype = DTYPE
        print ssDatetype

    elif fc == "VE_WEED_P":
        datetypedetector()
        vwDate = newList
        print fc + " = " + str(vwDate)
        vwDatetype = DTYPE
        print vwDatetype

    else: 
        datetypedetector()
        vrDate = newList
        print fc + " = " + str(vrDate)
        vrDatetype = DTYPE
        print vrDatetype
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:37:31+00:00Added an answer on May 31, 2026 at 6:37 am

    As written, myvariable is only defined within the scope of myfunction.
    To make the value in that variable available outside of the function you can return it from the function:

    def myfunction():
        myvariable = "Some Text"
        return myvariable
    

    And then use it later like this:

    for something in somethinglist:
        if something == iteminsomethinglist:
            mynewvariable1 = myfunction()
    

    Edit: new information added to question.

    Your indentation seems slightly off, but that may just be copy-paste trouble.
    I think what you want to do is something like this:

    1. Call the datetypedetector function taking fc as an argument.
    2. Return DTYPE from that function for later use.

    So first change the function signature to:

    def datetypedetector(fc):
                         ^^
    

    And the final statement in datetypedetector to:

        return DTYPE
    

    And then pass fc as an argument when you call it, and the final step is to get the DTYPE back from the function by assigning to it datetypedetector‘s return value.

    for fc in fcList:
        if fc == "SO_SOIL_P" or fc == "AS_ECOSITE_P":
            DTYPE = datetypedetector(fc)
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set the HttpRequestHeader for a HttpWebRequest like so: new HttpWebRequest().Headers.Add(HttpRequestHeader.UserAgent, Mozilla/4.0);
I am trying to set a local variable to an existing binding def foo_callback
I am currently trying to set up a new visual studio 2008 solution while
I'm trying to set the image of a new view. I set the controller's
I'm trying to set the group type for a new Active Directory Entry via
Given a set Paris New York London New York Paris Paris I'm trying to
I'm new to cookies, and im trying to set a cookie where to store
I'm painfully new to PHP, and was trying to set up phpBB on my
I'm a bit new to Papervision3D, and I'm trying to set the alpha level
I am new to EC2 and am trying to get it set up so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.