-2

I'm very new to python and have wrote a simple script to send plaint txt emails via SMTP (Ref: https://realpython.com/python-send-email/). I was wondering if I would be able to in some way include a hyphen as variable name but i'm not having any luck seeing if it's possible. My understanding is that you are cannot use hyphens in python but is there a work around?

The idea is to be able to apply a region as an addtional parameter of the smtp host, however, the regions in aws regions can be quite long to type, so abbreviated would have been great.

For ex. email-smtp.ap-southeast-2.amazonaws.com is one of the regions in AWS, could this be used as just ap-southeast-2 ?

python3 send-email.py -username -password --region ap-southeast-2

I've got everything working but just this.. Open to ideas too!

Chris Williams
  • 32,215
  • 4
  • 30
  • 68
Maverick32
  • 15
  • 7
  • 3
    Why would you need that to be a variable name? Command line arguments are strings, not variables. – Barmar Feb 08 '21 at 18:26
  • 1
    no, you cannot use hyphens in variable names, but `ap-southeast-2` looks like an argument (string) to your script, what's the need for creating a variable with that exact name? – goto Feb 08 '21 at 18:26
  • I'm very new to Python and any programming language in all honesty. Getting this to work was a miracle in itself. Thanks for the comments. – Maverick32 Feb 08 '21 at 18:33

1 Answers1

2

Sadly, you cannot use a hyphen in a variable or parameter name. You are restricted to the following rules

1st character

'_a-zA-Z' You can start a variable name using any letter or an underscore

All other characters

'[_a-zA-Z0-9]*' Same as first character, plus numbers.

Why?

The hyphen is the subtraction operator. Imagine you tried to create a function is-even(foo):

def is-even(foo):
    return('bar')

To python interpreters, it looks like you are trying to define a function of the keyword is minus some function even with the param foo. This type of response would be nearly impossible to avoid, hence hyphens are disallowed from variable and function names in nearly all languages.

CATboardBETA
  • 418
  • 6
  • 29