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
class Computer:
def _inti_(self, storage, color , system):
no_of_Computer = 0
self.storage = storage
self.color = color
self.system = system
Computer.no_of_Computer +=1
def describe (self):
print(f'my storage is {self.storage} and my color is{self.color} and my system is {self.system}')
Computer_1 = ("1TB , silver , windows ")
Computer_2 = (" 4TB , black , linux")
Computer_3 = (" 9TB , white ,mac ")
Computer_1.describe()
Computer_1, Computer_2 and Computer_3 aren't Computer instances, they are just strings (surrounded by parentheses). You need to call the Compueter's constructor to create new instances of it. Also, note that each argument should be its own string, not a single string with commas in it.
Additionally, note that a constructor is defined by the method __init__ (note the double undescores), not _inti_:
class Computer:
def __init__(self, storage, color , system):
no_of_Computer = 0
self.storage = storage
self.color = color
self.system = system
Computer.no_of_Computer +=1
def describe (self):
print(f'my storage is {self.storage} and my color is{self.color} and my system is {self.system}')
Computer_1 = Computer("1TB", "silver", "windows")
Computer_2 = Computer("4TB", "black", "linux")
Computer_3 = Computer("9TB", "white", "mac")
Computer_1.describe()
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 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
So I will have to finish a half-done code to get the desired output.
the half-done code goes as follows AND I AM NOT ALLOWED TO CHANGE THIS CODE:
class Wadiya():
def __init__(self):
self.name = 'Aladeen'
self.designation = 'President Prime Minister Admiral General'
self.num_of_wife = 100
self.dictator = True
the desired output goes as follows:
Part 1:
Name of President: Aladeen
Designation: President Prime Minister Admiral General
Number of wife: 100
Is he/she a dictator: True
Part 2:
Name of President: Donald Trump
Designation: President
Number of wife: 1
Is he/she a dictator: False
Now to get this output, I will have to use the same object which is wadiya in this case to change the values of the instance variables. Then print if it affected the previous values of Part 1. If it did, I'll have to print 'Previous information lost' otherwise I'll have to print 'No, changing had no effect in previous values.'
Now my question is, how can I change the values of the instance variables using the same object? This is what I've done, but I don't think this what the question has asked me to do. What do you think? Am I on the right track? Here's my approach:
class Wadiya():
def __init__(self):
self.name = 'Aladeen'
self.designation = 'President Prime Minister Admiral General'
self.num_of_wife = 100
self.dictator = True
def my_method(self):
print('Name of the President:', self.name)
print('Designation:', self.designation)
print('Number of wife:', self.num_of_wife)
print('Is he/she a dictator:', self.dictator)
def change_values(self, name, designation, num_of_wife, dictator):
self.name = name
self.designation = designation
self.num_of_wife = num_of_wife
self.dictator = dictator
print('Part 1:')
wadiya = Wadiya()
wadiya.my_method()
print('Part 2:')
wadiya = Wadiya()
wadiya.change_values('Donald Trump', 'President', 1, False)
wadiya.my_method()
Question is a bit ambiguous why would you want to change all values of an instance. If you want you can reassign new instance to same variable just pass arguments to init instead of change_method
if you want default values to class then you don't need to do init and then change values.
def __init__(self, name: str = None): # None is default value
self.name: str = name if name else 'Aladeen'
For some reason if you want to change values of instanced objects then do
wadiya.name = 'Donald'
what you are doing will work, but generally not suggested
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 want to create a button in my PyQt5 app, which would change the window title name to a new value.
I tried to make a custom function in class PushButton(QPushButton) which would tell MainWindow to setWindowTitle to a new name. Then I connected the .clicked signal within PushButton class to that function, however, whenever I press the button the application crashes.
I am kind of new, so if I got any class/function relationship wrong, please correct me.
What am I missing here?
windowTitle : QString
This property holds the window title (caption)
This property only makes sense for top-level widgets, such as windows and dialogs. If no caption has been set, the title is based of the windowFilePath. If neither of these is set, then the title is an empty string.
setWindowTitle(const QString &)
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Change the window title name')
self.resize(300, 150)
self.lineEdit = QLineEdit(self)
self.lineEdit.move(30, 65)
button = QPushButton("Button", self)
button.clicked.connect(self.onClicked)
button.move(190, 65)
def onClicked(self):
self.setWindowTitle(self.lineEdit.text()) # <---
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
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 am trying to create a proxy checker in python3 and everything is working fine until than i didn't introduce multi-threading in it to make it fast now it is giving me error and i am not able to understand why it is so
import requests
import threading
#DECLARING ALL VARIABLES
proxy_api="https://api.proxyscrape.com/?request=getproxies&proxytype=http&timeout=50&country=all&ssl=all&anonymity=all"
raw_proxy = []
live_proxy = []
#Declaring ALL COUNTERS
proxy_Counter = 0
def main():
pass
def fetch_proxy():
global raw_proxy
res = requests.get(proxy_api)
raw_proxy = res.text.splitlines()
print(len(raw_proxy))
return raw_proxy
def check_proxy():
global raw_proxy
global live_proxy
global proxy_Counter
while proxy_Counter < len(raw_proxy):
try:
proxyDict = {
"https" : "https://"+raw_proxy[proxy_Counter],
"http" : "http://"+raw_proxy[proxy_Counter],
}
res = requests.get("http://httpbin.org/ip",proxies=proxyDict,timeout=3)
print(f"Proxy Live {raw_proxy[proxy_Counter]}")
live_proxy.append(raw_proxy[proxy_Counter])
proxy_Counter+=1
except Exception as e:
print(f"Dead Proxy {raw_proxy[proxy_Counter]}")
proxy_Counter+=1
print(len(live_proxy))
return live_proxy
fetch_proxy()
threads = []
for _ in range(10):
t = threading.Thread(target=check_proxy)
t.start()
threads.append(t)
for t in threads:
t.join()
You didn't provide any error or stacktrace but it looks like you're getting an IndexError, this is because your loop is incorrect. Imagine you got back 100 proxies, your current loop will go from 0 to 100, however, that is 101 entries and not 100; because 0 is the first index. A quick solution is to change this line so that it is one less (0-99 = 100 iterations):
while proxy_Counter < len(raw_proxy) - 1:
However, if your aim is to speed up the process of checking the proxies, your code will have the opposite effect because for each thread you create, you're checking each proxy again so now you have x10 the redundancy. It would be better to use a ThreadPoolExecutor and evenly distribute the proxies to your threads and set a maximum amount of threads so that you're not overloading the server.
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