I am mostly familiar with Java and am in the process of learning Python. I have been playing around with OOP in Python and am slightly confused how it keeps track of data types. What I am confused about is the get_average_grade() function in the image below. In the for loop how does Python know that student is an object of type Student? Note: the list self.students was assigned to be an empty list in the class' init.
Asked
Active
Viewed 628 times
-2
-
Check out [How and when does Python determine the data type of a variable?](https://stackoverflow.com/questions/53449112/how-and-when-does-python-determine-the-data-type-of-a-variable) – DarrylG May 19 '20 at 22:41
-
1Please include any code as text in the question, not as an image. – mkrieger1 May 19 '20 at 22:41
-
1It's unclear what you are asking. Python is a dynamically typed language, the *type* of an object is determined at runtime, so a lot of stuff from a statically typed language like Java isn't going to translate directly. What *exactly* are you asking? – juanpa.arrivillaga May 19 '20 at 22:44
-
1Python does not need to know that `student` is an object of type `Student`. See https://realpython.com/lessons/duck-typing/ – jarmod May 19 '20 at 22:45
-
It is very important to understand, in Python, *variables* don't have a data type. They can refer to *any* type of object – juanpa.arrivillaga May 19 '20 at 22:46
-
Helps to better understand the [Python Interpreter vs. JVM](https://stackoverflow.com/questions/441824/java-virtual-machine-vs-python-interpreter-parlance/1732383#1732383) to see why Python typing is so dynamic. – DarrylG May 19 '20 at 22:49
-
@juanpa.arrivillaga I am wondering how Python knows that student (the iterator) has access to the method get_grade() defined in the Student Class? – Max May 19 '20 at 22:55
-
@Max it doesn't know, until it tries to execute that method. – jarmod May 19 '20 at 23:02
-
@Max it doesn't "know". It tries to access that attribute and call the resulting object. Try passing something else, it will throw a runtime error. Note, in that isn't a *iterator*. But that's a semantic quibble – juanpa.arrivillaga May 19 '20 at 23:09
-
And note, it doesn't have to be a student object, it will accept any object, and will not throw an error for any object with a `get_grade()` method. – juanpa.arrivillaga May 19 '20 at 23:15
2 Answers
4
Python variables don't actually have data types. Python variables are simply symbolic names for a reference (or pointer) to an object. That object has a value (for example 23 or "cat") and a type (for example str or int or Student). A name can be associated with an int one second and with a Student the next second.
You can call get_grade()
on any variable. If that variable doesn't have a get_grade()
method then the result is an AttributeError exception.
Some helpful background that all Python programmers should read:

jarmod
- 71,565
- 16
- 115
- 122
0
Python is smart enough to know what type you want a variable to be when you assign it. Check this out.
student = 'Hi Mom!'
print (student)
student = 7
print (student)
student = [1, 2, 3]
print (student)
At any time 'student' can be a string, integer, list, dictionary etc, etc...

bashBedlam
- 1,402
- 1
- 7
- 11