Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Python question on setString()

    Dear All,

    consider the following fragment:

    Code:
    clear all
    version 18.0
    python: 
    
    import requests
    from sfi import SFIToolkit, Scalar, Macro
    
    def ipinfo1():
        SFIToolkit.rclear()
        Scalar.setString("r(country)","test") # where does this value go??
        Scalar.setValue("r(district)",1)
        Macro.setGlobal("r(region)","some region")
        print(Scalar.getString("r(country)")) # prints nothing
        print("-------- done -------")
    end
    
    python: ipinfo1()
    scalar list
    return list
    sreturn list
    ereturn list
    According to the documentation, the method Scalar.setString() should save the value to a Stata string scalar, which can be, in particular, r()-value, which is what I need. I see the command doesn't affect the r()-values, though it doesn't result in an error.

    Q1: Where is the passed value "test" saved?
    Q2: Why is this value is not subsequently retrieved with Scalar.getString() ?


    Output:

    Code:
    . python: ipinfo1()
    
    -------- done -------
    
    . scalar list
    
    . return list
    
    scalars:
               r(district) =  1
    
    macros:
                 r(region) : "some region"
    
    . sreturn list
    
    . ereturn list
    
    . 
    end of do-file
    
    .

  • #2
    Originally posted by Sergiy Radyakin View Post
    . . . the method Scalar.setString() should save the value to a Stata string scalar, which can be, in particular, r()-value . . .
    The documentation in the user's manual for return ā€” Return stored results under the section heading Storing results in r() (p. 476) says:
    ā— return scalar name = exp evaluates exp and stores the result in the scalar return(name). exp must evaluate to a numeric result or missing.
    That is, you cannot store a string scalar in r(). I happen to know this because I just got stung by it a few days ago.

    Comment


    • #3
      HTML Code:
      you cannot store a string scalar in r()
      Thank you very much. I have a subtle feeling that I already lived through the same puzzle with java in the past. The asymmetry in handling different types of data (strings vs numeric) is rather peculiar.

      Still, Iā€™d rather have Stata issue an error message in this case, instead of silently executing a nonsense code.

      Best, Sergiy

      Comment

      Working...
      X