How can it happens? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

18th Jul 2021, 4:27 AM
UnTentetive
8 Answers
+ 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.
18th Jul 2021, 4:36 AM
Calvin Thomas
Calvin Thomas - avatar
+ 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
18th Jul 2021, 5:43 AM
David Carroll
David Carroll - avatar
+ 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
18th Jul 2021, 5:16 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas Means it returns a string !
18th Jul 2021, 4:45 AM
UnTentetive
+ 1
Calvin Thomas Please tell me that if re.match() returns an object then how "If statement" is working?
18th Jul 2021, 5:04 AM
UnTentetive
+ 1
David Carroll Hmm thanks
18th Jul 2021, 10:48 AM
UnTentetive
+ 1
Calvin Thomas Yes now i understood 😀
18th Jul 2021, 10:51 AM
UnTentetive
0
Amit Ranjan Sahoo Well, since 'string' means an object in Python, yeah.
18th Jul 2021, 4:56 AM
Calvin Thomas
Calvin Thomas - avatar