-1

To rephrase my question for below:

What is the point of telling the parameter is string when it cannot convert to string when integer was entered as input?

I understand we can use str() to convert integer to string but that's not the answering I'm looking for.


After running the code by entering gss inside the paramteter, I received 1. However, when I look up the type of this results, it shows as NoneType.

Why is this not string?

gss=1
convert_gss_to_str(gss)
1
type(convert_gss_to_str(gss))
Nonetype

I ran the below codes thinking that the integer 1 will be converted to string '1'.

However, I received this error: TypeError: convert_gss() missing 1 required positional argument: 'gss'

Any suggestion what I am doing wrong?

gss = 1

def convert_gss_to_str(gss: str):
    print(gss)

convert_gss_to_str()
learnnabee
  • 31
  • 6
  • 2
    You need to pass an argument into the function. `convert_gss_to_str(gss)`. Also it would make sense to return `str(gss)` since that seems to be the purpose of the function. – alec Feb 21 '20 at 07:47
  • Does this answer your question? [Converting integer to string?](https://stackoverflow.com/questions/961632/converting-integer-to-string) – mkrieger1 Feb 21 '20 at 07:50
  • @alec Hi alec. The type that I received back is NoneType if I do not return with str(gss). Why do I have to specify when I already told the function that gss is string. Even when I got back result, there is no error in the function. Can you advise? – learnnabee Feb 21 '20 at 07:50
  • The argument type is optional in python, unlike other languages. In fact it won't enforce the object you pass to it. If you want to receive a specific value from a function you must specify what it returns. By default, the return type is `None`, which is why you saw `Nonetype`. When you explicitly return a string, the return type will be `str`. – alec Feb 21 '20 at 07:57
  • @mrieger1 no I understand we can use built-in function str() to convert integer to string. That's not the answer I'm looking for. I have rephrased my question. – learnnabee Feb 21 '20 at 08:01
  • @learnnabee static typing does not auto-cast values. The built-in function str() does convert to string while the `def func(arg0: str):` limits the types of arguments; those two are very different. – tafaust Feb 21 '20 at 09:31
  • 1
    @ThomasHesse It doesn't limit the types of arguments, it's only a hint. – mkrieger1 Feb 21 '20 at 11:07
  • @mkrieger1 Thanks for correcting! Much appreciated. – tafaust Feb 21 '20 at 11:23

3 Answers3

1
def convert_gss_to_str(gss: str):
    print(gss)

This function takes one non-optional parameter gss and does not return anything, therefore its return type is Nonetype. If you want to do actual conversion you can use builtin function str() as suggested by Sawel.

Type conversion is not really necessary for print() as it will print integers anyways

Ach113
  • 1,775
  • 3
  • 18
  • 40
  • What is the point of having parameter set up as str? What is the purpose for that then when it does not convert? Is it only declaring that it will be string? But in this case, I entered in integer and why did python pass my integer when I told specifically that it will be string? – learnnabee Feb 21 '20 at 07:54
  • 1
    It is just for clarification that that parameter is expected to be string, it does not do conversion. You can test it by printing `print(type(gss))` inside the function and it will print `` – Ach113 Feb 21 '20 at 07:55
  • @Arh1113 so other than clarification, there is no other purpose than stating gss will be string? – learnnabee Feb 21 '20 at 08:00
1
def convert_gss_to_str(gss: str):
    ...

It's just a type hint.

For more information, read PEP484

While these annotations are available at runtime through the usual annotations attribute, no type checking happens at runtime. Instead, the proposal assumes the existence of a separate off-line type checker which users can run over their source code voluntarily. Essentially, such a type checker acts as a very powerful linter.

kyungmin
  • 474
  • 1
  • 4
  • 9
0

Just use the builtin function str.

>>> gss = 1
>>> str(gss)
"1"
Sawel
  • 929
  • 7
  • 17