There is no difference. Python changed the text representation of type objects between python 2 (Types are written like this: <type 'int'>.) and python 3 (Types are written like this: <class 'int'>.). In both python 2 and 3, the type of the type object is, um, type:
python 2
>>> type(type('a'))
<type 'type'>
python 3
>>> type(type('a'))
<class 'type'>
And that's the reason for the change... the string representation makes it clear that the type is a class.
As for the rest of your problem,
for ID in response:
response is a string and enumerating it gives the characters in the string. Depending on the type of response you may want to use and HTML, JSON or other parser to turn it into python objects.
I'm still a beginner in python and my English is not really good, so I may have a little bit of understanding the explanation given in my course.
So I have this code:
class Apple:
def __init__ (self,color,flavor):
self.color=color
self.flavor=flavor
def __str__(self):
return "This apple is {} and its flavor is {}".format(self.name,self.flavor)
jonagold=Apple("red","sweet")
print(jonagold)The only purpose I see in using the __str__ method, I don't have to write the method in the print () line. If I changed the __str__ method with:
def say(self):
return "This apple is {} and its flavor is {}".format(self.name,self.flavor)I would just need to type
print(jonagold.say())
And I would get the same answer. What are the other purpose of using __str__? In what situation would I need to use the __str__?
Thank you :)
python - What is the difference between <class 'str'> and <type 'str'> - Stack Overflow
python - How to use __str__ function to return a string representation of a class - Stack Overflow
string - Confused about __str__ on list in Python - Stack Overflow
Line 9 - Class 'str' does not define '__sub__', so the '-' operator cannot be used on its instances
There is no difference. Python changed the text representation of type objects between python 2 (Types are written like this: <type 'int'>.) and python 3 (Types are written like this: <class 'int'>.). In both python 2 and 3, the type of the type object is, um, type:
python 2
>>> type(type('a'))
<type 'type'>
python 3
>>> type(type('a'))
<class 'type'>
And that's the reason for the change... the string representation makes it clear that the type is a class.
As for the rest of your problem,
for ID in response:
response is a string and enumerating it gives the characters in the string. Depending on the type of response you may want to use and HTML, JSON or other parser to turn it into python objects.
As mentioned by the commenters. In python3:
>>>st = 'Hello Stack!'
>>>type(st)
<class 'str'>
But in python2:
>>>st = 'Hello Stack!'
>>>type(st)
<type 'str'>
So the behavior that you are seeing is entirely expected. As to looping over a string, a for loop over a string will iterate over the string character by character. If you want to iterate over each line in the string, you usually do something like split on \n or some regex designed to split on the line separators in the URL response. Below is a simple for loop over the list resulting from split
response = urllib.request.urlopen(req).read().decode()
lines = response.split('\n')
for x in lines:
st = x.strip()
# do some processing on st
Python has two different ways to convert an object to a string: str() and repr(). Printing an object uses str(); printing a list containing an object uses str() for the list itself, but the implementation of list.__str__() calls repr() for the individual items.
So you should also overwrite __repr__(). A simple
__repr__ = __str__
at the end of the class body will do the trick.
Because of the infinite superiority of Python over Java, Python has not one, but two toString operations.
One is __str__, the other is __repr__
__str__ will return a human readable string.
__repr__ will return an internal representation.
__repr__ can be invoked on an object by calling repr(obj) or by using backticks `obj`.
When printing lists as well as other container classes, the contained elements will be printed using __repr__.
I don't quite understand why the - sign on Line 9 is giving me issues.
Also on line 7 it's giving me the error "Expected type 'int', got 'str' instead" for both "low" and "high". Any advice? Thank you in advance!
import random
import math
low = input("Enter lower bound: ")
high = input("Enter higher bound: ")
x = random.randint(low, high)
print("\n\t You only have",
round(math.log(low - high + 1, 2)),
"chances to guess!\n")
Actually the same mechanism as for object instances applies for types. Types are just objects themselves, so they are converted to strings by calling the __str__() method on their type, which is called the "metaclass". So you have to overwrite the __str__() method on the metaclass:
class fancytype(type):
def __str__(self):
return self.__name__
class ham(object):
__metaclass__ = fancytype
print ham
prints
ham
You can also set the default metaclass for a whole module like this
class fancytype(type):
def __str__(self):
return self.__name__
__metaclass__ = fancytype
class ham:
pass
print ham