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
Well, I just noticed that variables in if name == 'main': are shared to the classes in the same file - why is this? I don't recall having this issue in python2...
class A:
def __init__(self, b):
self.b = b
def func_a(self):
return d
if __name__ == '__main__':
classA = A(1)
d = 2
print(classA.func_a())
prints out 2.
What's the reasoning?
this definitely also happens in python2
and is very simple: declaring variables outside of functions/classes makes them global and when python searches for variables with the name d it doesn't find it in the local scope but it does find the global variable
Related
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 2 months ago.
Improve this question
I'm noobie/beginner at Python
I was creating this script
I tried to curl a URL and got a response(A)
now I want to get the response(B) from that response A
result = os.popen("curl https://raw.githubusercontent.com/SirdLay/test/main/test.txt").read()
index = os.popen("curl [what to be used here?]")
response1 = os.popen("curl https://raw.githubusercontent.com/SirdLay/test/main/test.txt").read()
url = response1[<key>]
# key is some key in the response1.
# You can print the response1 to see the object.
# For your particular case you dont need a key.
# so you can use
url = response1
response2 = os.popen("curl " + url).read()
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
cur.execute(command, tuple(values))
if (fetch := cur.fatchone()) is not None:
return fetch[0]
#bot_has_permissions(manage_roles=True)
#has_permissions(manage_roles=True, manage_guild=True)
async def unmute_members(self, ctx, members: Greedy[Member], *, reason: Optional[str] = "لا يكثر هرجك"):
if not len(members):
await ctx.send("!unmute #member [reason]")
else:
await self.unmute(ctx, members, reason=reason)
AttributeError: 'sqlite3.Cursor' object has no attribute 'fatchone'
Typo.
if (fetch := cur.fatchone()) is not None:
should be
if (fetch := cur.fetchone()) is not None:
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
I created a simple program to python that check if this X is expired.
import datetime
expired_on = datetime.datetime.now() + datetime.timedelta(0, 20) # add 20 seconds for expiration time
while True:
X = datetime.datetime.now()
if expired_on == X:
print(f"this {X} is expired.")
break
but it didn't break after 20 seconds.
This will be much cleaner.
import datetime
expired_on = datetime.datetime.now() + datetime.timedelta(seconds=20) # Add 20 seconds for expiration time
while True:
now = datetime.datetime.now()
if expired_on <= now:
print('Expired.')
break
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am making a game where you shall shoot down diffrent boxes with diffrent nummber and text on and was wondring if you can write text as a sprite
class Text(pygame.sprite.Sprite):
def __init__(self, text, size, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", size)
self.textSurf = self.font.render(text, 1, color)
self.image = pygame.Surface((width, height))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])
I hope that helps, this will draw text centered on surface in a sprite
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 8 years ago.
Improve this question
>>> compile("""
def some_function(x):
return x+2
some_function""",'compiled function','single')
Traceback (most recent call last):
File "<pyshell#3>", line 4, in <module>
some_function""",'compiled function','single')
File "compiled function", line 4
some_function
^
SyntaxError: unexpected EOF while parsing
If you want to compile a multi-statement string with compile, single should be exec. Also, after you compile the code, you have to execute it and capture the globals in order to access the created function:
def anonymous(code):
# To fix the indentation
fixed_code = '\n'.join(line[4:] for line in code.splitlines())
_globals = {}
exec(compile(fixed_code, '<string>', 'exec'), _globals)
if 'f' not in _globals:
raise ValueError('You must name your function "f"')
return _globals['f']
anonymous('''
def f(x):
return x + 2
''')(12)
Question is not very clear, but is this the example you want?
>>> c=compile('''\
... def some_function(x):
... return x+2
... print(some_function(5))
... ''','<string>','exec')
>>> exec(c)
7
>>> c=compile('7+2','<string>','eval')
>>> eval(c)
9