0
How can it happens?
If a match found in the string then the match() function will return an object representing the match. The match is a string, am i right ? How a string can be an object?
8 Réponses
+ 2
Amit Ranjan Sahoo In low-level languages like 'C', there is no data-type known as a 'string'; they're just represented as an array of characters, which is what it actually is. In Python, 'str' is a class (a variable that stores the reference to the string-class, accurately speaking). This class includes all the string properties and methods like __class__, isalpha(), etcetera along with the character array which makes up the string (a lot more are there). Whenever a string is created, an instance of this class is made in the heap and the reference to it returned. Everything is an object in Python.
P.S. I guess that the real question was to know what the match function returns. It returns a match object, btw.
+ 5
Amit Ranjan Sahoo The "match object" is NOT a string. Rather it's an instance of the Match class:
https://docs.python.org/3/library/re.html#match-objects
You can see this better in a simple example:
https://code.sololearn.com/coW3Ue21BrNk/?ref=app
+ 2
Amit Ranjan Sahoo All objects other than "", 0, [], {}, None, 0.0 and False have a boolean value of 1 (True). So,
if 'adf':
print("This will be printed")
else:
print("This won't be printed")
# Hope this helps
+ 1
Calvin Thomas
Means it returns a string !
+ 1
Calvin Thomas
Please tell me that if re.match() returns an object then how
"If statement" is working?
+ 1
David Carroll
Hmm thanks
+ 1
Calvin Thomas
Yes now i understood 😀
0
Amit Ranjan Sahoo Well, since 'string' means an object in Python, yeah.