<p>In C++, you can define variables with <code>auto</code> instead of giving it a default type such as <code>int</code>. Like this (where <code>foo</code> is an int):</p>
<pre><code>int x = foo
auto y = foo
</code></pre>
<p>Both contain <code>foo</code>.</p>
<p>In Python, you can define a variable without specifying the data type. Like this:</p>
<pre><code>x = foo
</code></pre>
<p>The compiler determines the type of the variable at runtime based on the type of data assigned.</p>
<p>What are the differences between using an <code>auto</code> datatype and dynamic typing?</p>

<variables><data-types>
What is the difference between having an auto type and having dynamic typing?
CC BY-SA 4.0
2023-09-19T01:15:36.983Z
For one, and I think it's the most mentioned disadvantage for C++, figuring out what the type of the variable actually is can get pretty hard if your IDE isn't top notch. Especially if `foo` is a templated function that can depend on its' parameter values.
kouta-kun
CC BY-SA 4.0
2023-09-19T13:31:20.333Z
This question may be naively posed, but answers need not be. This may already have answers elsewhere in Internet. I am most interested in differences from the programmer's (ie user of the language) point of view, especially the implications for how code is structured/organised.
Pablo H
<p><code>auto</code> is an <strong>inferred</strong> type but it's still a <strong>static</strong> type. The difference is that the compiler knows what every value's type is in a statically-typed language (including the actual type of the <code>auto</code> values), but in a dynamically-typed language the type is only known at runtime. Also, variables in a statically-typed language have one fixed type even if they're declared with <code>auto</code>, but variables in a dynamic language can change types during program execution.</p>
<hr />
<p>Consider the following C++ code:</p>
<pre class="lang-cpp prettyprint-override"><code>int main() {
  auto foo = &quot;hello&quot;;
  puts(foo / 5);
}
</code></pre>
<p>This code will fail to <strong>compile</strong>, because you can't divide a <code>const char*</code> by an integer. Notably, even though <code>foo</code> is declared with type <code>auto</code>, the compiler knows that it's actual type is <code>const char*</code>. In fact, using <code>const char* foo = &quot;hello&quot;</code> gives the exact same semantics, <code>auto</code> is just syntax sugar for the programmer.</p>
<p>Now, consider the corresponding Python code:</p>
<pre class="lang-python prettyprint-override"><code>def main():
  foo = &quot;hello&quot;
  print(foo / 5)
</code></pre>
<p>This code will fail at <strong>runtime</strong> if/when <code>main</code> is called. Python doesn't statically check types, so while you also can't divide a string by an integer, it's only detected when <code>foo / 5</code> actually runs.</p>
<hr />
<p>Failing at compile-time is generally regarded as better, but there are other drawbacks to static types (see: <a href="https://langdev.stackexchange.com/questions/587/what-are-the-pros-and-cons-of-static-typing/655#655">&quot;What are the pros and cons of static typing?&quot;</a>). For example, you can't assign an <code>auto</code> variable a value of a different type than it was declared with, because it still has a single consistent (&quot;static&quot;) type, the type is just inferred.</p>
<pre class="lang-cpp prettyprint-override"><code>int main() {
  // This is OK, `foo` has type `const char*`
  auto foo = &quot;hello&quot;;
  foo = &quot;world&quot;;
  // So is this, `bar` has type `int`
  auto bar = 42;
  bar = 45;
  // However, this doesn't compile, because `foo` has already been assigned type `const char*`
  foo = 48;
}
</code></pre>
<p>But you can assign differently-typed values the same variable in a language with dynamic types, because <code>foo</code> has no consistent type (it's type is &quot;dynamic&quot;).</p>
<pre class="lang-python prettyprint-override"><code># This code runs as expected, with no errors
foo = &quot;hello&quot;
print(foo)
foo = 42
print(foo / 2)
</code></pre>

CC BY-SA 4.0
2023-09-19T14:09:52.597Z
2023-09-19T14:09:52.597Z
tarzh
CC BY-SA 4.0
2024-04-13T14:20:56.797Z
(IMHO, reusing variable names like that, especially for different types, is a code smell and risks confusion.  So that's not a very strong argument.)  Other advantages of static typing include lookup/completion in an IDE, performance/optimisation, and safer refactoring.  One of the few traditional disadvantages was verbosity — but that's dwindling with type inference in modern languages such as Kotlin, which have all the safety and expressivity of static typing but much of the conciseness and fast development of dynamic typing.
gidds
# What is the difference between having an auto type and having dynamic typing?
Posted by: username_0 on 2023-09-19
In C++, you can define variables with auto instead of giving it a default type such as int. Like this (where foo is an int):
int x = foo
auto y = foo

Both contain foo.
In Python, you can define a variable without specifying the data type. Like this:
x = foo

The compiler determines the type of the variable at runtime based on the type of data assigned.
What are the differences between using an auto datatype and dynamic typing?
**Tags:** variables data-types
> **Comments:**
> **username_1**: For one, and I think it's the most mentioned disadvantage for C++, figuring out what the type of the variable actually is can get pretty hard if your IDE isn't top notch. Especially if `foo` is a templated function that can depend on its' parameter values.  
> **username_2**: This question may be naively posed, but answers need not be. This may already have answers elsewhere in Internet. I am most interested in differences from the programmer's (ie user of the language) point of view, especially the implications for how code is structured/organised.  
---
## 1 answers
### Answer by username_3 on 2023-09-19. Score: 12 | Accepted answer
auto is an inferred type but it's still a static type. The difference is that the compiler knows what every value's type is in a statically-typed language (including the actual type of the auto values), but in a dynamically-typed language the type is only known at runtime. Also, variables in a statically-typed language have one fixed type even if they're declared with auto, but variables in a dynamic language can change types during program execution.

Consider the following C++ code:
int main() {
  auto foo = "hello";
  puts(foo / 5);
}

This code will fail to compile, because you can't divide a const char* by an integer. Notably, even though foo is declared with type auto, the compiler knows that it's actual type is const char*. In fact, using const char* foo = "hello" gives the exact same semantics, auto is just syntax sugar for the programmer.
Now, consider the corresponding Python code:
def main():
  foo = "hello"
  print(foo / 5)

This code will fail at runtime if/when main is called. Python doesn't statically check types, so while you also can't divide a string by an integer, it's only detected when foo / 5 actually runs.

Failing at compile-time is generally regarded as better, but there are other drawbacks to static types (see: "What are the pros and cons of static typing?"). For example, you can't assign an auto variable a value of a different type than it was declared with, because it still has a single consistent ("static") type, the type is just inferred.
int main() {
  // This is OK, `foo` has type `const char*`
  auto foo = "hello";
  foo = "world";
  // So is this, `bar` has type `int`
  auto bar = 42;
  bar = 45;
  // However, this doesn't compile, because `foo` has already been assigned type `const char*`
  foo = 48;
}

But you can assign differently-typed values the same variable in a language with dynamic types, because foo has no consistent type (it's type is "dynamic").
# This code runs as expected, with no errors
foo = "hello"
print(foo)
foo = 42
print(foo / 2)
> **Comments:**
> **username_4** (2024-04-13): (IMHO, reusing variable names like that, especially for different types, is a code smell and risks confusion.  So that's not a very strong argument.)  Other advantages of static typing include lookup/completion in an IDE, performance/optimisation, and safer refactoring.  One of the few traditional disadvantages was verbosity — but that's dwindling with type inference in modern languages such as Kotlin, which have all the safety and expressivity of static typing but much of the conciseness and fast development of dynamic typing.  
---
