Underscores in method names [silly mistake] [closed] - python-3.x

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
EDIT: My mistake - there was another method name (with the underscore) at another place which had only a pass in its body. Stupid me. Please close.
I have a class with two methods, each returning a simple string. The only slightly qualitative difference between these two methods is that one has an underscore in its name while the other doesn't. However, the one with the underscore does not return the string when called. Using underscores in names of methods seems to follow PEP 8 guidelines regarding method names.
I have been reading the Python tutorial on classes thoroughly but it doesn't mention any difference in behaviour depending use of underscore inside the method name. If I remove the underscore, the string will be returned.
What's causing this?
Here's my code:
class Board:
def f(self):
return 'Hello'
def print_board(self):
return 'Hello'
I'm using Python 3.2.3.

It works fine for me with Python 3.2.1 and 3.3.0:
class Board:
def f(self):
return 'Hello'
def print_board(self):
return 'Hello'
b = Board()
print(b.f())
print(b.print_board())
Executed at Windows, it prints:
c:\tmp\_Python>c:/python32/python a.py
Hello
Hello
c:\tmp\_Python>c:/python33/python a.py
Hello
Hello
Possibly you wanted to write:
...
def print_board(self):
print('Hello')
and call it
...
b.print_board()

Related

Even when I have the return value, it still returns none? Python Code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
The goal of this code is to alphabetize and uppercase the inputted tuple of values. However, it is returning none when I run it. I find this odd since I have a return and I beilieve everything is in correct order. If you can help find the answer, thanks. Here is the code:
def sorter(*args):
args = " ".join(args)
uppercased = args.upper()
listed = list(uppercased)
sorted1 = listed.sort()
return sorted1
print(sorter('happy', 'apple', 'zain', 'freindly', 'jakob'))
Run your code in the Python Tutor Visualizer and step through it line by line, and you will see that listed.sort() doesn't return anything but instead mutates listed:
Before executing listed.sort():
After executing listed.sort():
The docs for list.sort also tell you that the list is sorted in-place, and the function signature doesn't have a return value.
The solution is therefore to use listed after sorting it, instead of creating a new variable sorted1.
(Note that there are other logical mistakes in your code which will prevent it from delivering the result you probably expected, even after this specific issue is fixed, but that's beyond the scope of this question and answer.)

regex to match the pattern in python3 using match method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to know the regex using re.match method to match below pattern.
some action 1.1.1.1 on sub.domain.com
Currently I'm using below code, which works fine but I want to strip of everything except sub.domain.com because using below code I'm getting something like <http:\\/\\/sub.domain.com|sub.domain.com>
some\s+action+\s+(.*)\s+on\s+(.*)
This is where you would use a capturing group. Notice you have two capturing groups in your question, but you only want one. Here would be an example of one where you get the captured group after the first four "words" (defined as one or more non-spaces plus a space):
(?:\S+\s){4}(.*)
Also note that in actual practice this wouldn't work too well -- what if "some action" is ten words? Instead you might want to do something more targeted such as a regex to match (in pseudocode) <IP> on (<domain>).
>>> import re
>>> s='some action 1.1.1.1 on sub.domain.com'
>>> re.match(r'(?:\S+\s){4}(.*)', s).group(1) # not using anchors, not sure if you need that?
# 'sub.domain.com'

Removing special characters in field name using groovy [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a field <Age (D.O.B)>.I want the result to be i,e Age in brackets (unable to see my tag here)
I am using groovy.Please help.
I tried escaping the characters but unable to.
def msgBodyModified21 = msgBodyOriginal.replaceAll('<Age'+\\s+'(D.O.B)>', '<Age>')
Your quoting on the regexp is wrong. Use:
"<Age (D.O.B)>".replaceAll(/<Age\s+\(D\.O\.B\)>/, "<Age>")

Unable to search simple pattern in string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Unable to guess why this is returning None eventhough string containing "-pg:2Fetchotp for login:" pattern is found within str1:
import re
str1='1592029830201;12:00:30-bid:6--pg:2Fetchotp for login: 70300002358'
ret = re.match('-pg:2Fetchotp for login:\s+(\d{10})',str1)
print(ret)
re.match matches the beginning of the string and doesn't search for the pattern in whole string. Also, there's a typo in your regex. Try the following:
import re
str1='1592029830201;12:00:30-bid:6--pg:2Fetchotp for login: 70300002358'
ret = re.match('.*-pg:2Fetchotp for login:\s+(\d{10})',str1)
print(ret)
With .* added, the regex can be matches the string starting from the beginning. And so the match is complete string in this case. Otherwise you can also use re.search which searches the string for first match:
import re
str1='1592029830201;12:00:30-bid:6--pg:2Fetchotp for login: 70300002358'
ret = re.search('-pg:2Fetchotp for login:\s+(\d{10})',str1)
print(ret)

Why this python code is showing error to check a palindrome no.? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Whenever I run is_palindrome('otto'), it shows the error 'IndexError: string index out of range'...However,when I just compile the code, it does not show any error as such. thanks for helping out
def middle(word):
return word[1:-1]
def is_palindrome(word):
if word[0] != word[-1]:
return False
elif len(word)<=1:
return True
else:
return is_palindrome(middle(word))
What is happening is that for strings of even numbers of chars, it will eventually run palindrome("") which throws an out of bounds error. Try switching len(word)<=1 and word[0] != word[-1]

Resources