Questions tagged as 'python-3.x'

2
answers

Is it good practice to always manage errors with exceptions?

I'm creating a game with Python 3.4 and Pygame, and I'm using the object-oriented paradigm. In my classes, especially in __init__ , I'm full type checkings , to prevent runtime errors. For example, this is my class Board...
asked by 09.12.2014 / 01:22
2
answers

How to multiply in Python without the multiplication operator?

I have a task and I am having difficulty completing it. What I did was this: m= int(input('Digite o primeiro fator:')) n= int(input('Digite o segundo fator:')) def multiplica(numero): while m > 0: print (n + multiplica (m-1) n)...
asked by 16.05.2018 / 14:59
2
answers

Open, edit and save a binary file in Python3

You can open a binary file and read its bits in Python3, edit and save a new binary. If so, how?     
asked by 03.01.2017 / 16:38
1
answer

What does __ * (any word) __ or _ * mean in Python?

Reading a book from time to time it puts __init__ or __init and I do not know what those "_" means, I tried to search the internet for some explanations, but I ended up making it harder, could someone help me explaining more practi...
asked by 16.12.2017 / 11:14
2
answers

Why can not f-strings be used as a docstring?

According to PEP 257 , you have:    A docstring is the literal string that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object....
asked by 17.08.2018 / 20:00
1
answer

How to include in Python a function made in C?

I want to use a function made in C . Example: I have a function int swap(){ printf("lista"); } And I want to call it Python ...     
asked by 23.10.2015 / 18:32
4
answers

How to limit the generation of pseudorandom numbers to nonzero and non-repeating numbers?

I wish the numbers did not repeat themselves and were nonzero. import random Sort1 = int(60 * random.random()) Sort2 = int(60 * random.random()) Sort3 = int(60 * random.random()) Sort4 = int(60 * random.random()) Sort5 = int(60 * ra...
asked by 27.12.2016 / 23:32
3
answers

Measure the execution time of a function

How can I measure the runtime of a function in Python? In C #, I use the class Stopwatch() in this way var sw = new Stopwatch(); sw.Start(); AlgumaFuncao(); sw.Stop(); WriteLine(sw.ElapsedTicks);     
asked by 10.11.2015 / 14:41
1
answer

How to make a "deep copy" in Python?

Let's say I have the following code: class Foo: pass foo_list = [Foo() for _ in range(10)] How can I proceed to create a copy of foo_list without references to both the list and the objects contained therein being passed to the...
asked by 19.10.2017 / 20:38
2
answers

In Python, is there any rule or advantage over using 'Self'?

Consider the examples below: Example 1: > class PrintText(): > def __init__(self): > text = 'Funciona!' > self.printa(text) > > def printa(self, text): > print(text) Example 2...
asked by 09.07.2014 / 17:38