Python constructor overload with different number of parameters [duplicate] - python

This question already has answers here:
How to overload __init__ method based on argument type?
(10 answers)
Python function overloading
(19 answers)
Closed 4 years ago.
I am trying to build a complex number calculator in python. The number will be represented by a class with two fields. The first constructor I want to use looks like this:
def __init__(self,real,imag):
self.real = real
self.imag = imag
but i would like to use another, where the only parameter will be a string representing the whole number:
def __init__(self,string_number):
How to implement the overload that allows to use different number of parameters? I am more used to C++, where such thing was easy.
Thanks a lot!
EDIT:
Question marked as duplicate: I did not find any answer regarding different number of parameters. The error I get when trying to build a new instance of the class is about to few arguments.

Related

Overwrite sum behavior on a custom class inhering from an iterable (list) [duplicate]

This question already has answers here:
How to implement built-in sum() of the class?
(2 answers)
Define "sum" for a class using non-associative addition
(2 answers)
Closed 9 months ago.
Is there any way I can over write the sum() method to act differently on a list?
I expected I would be able to find a dunder method that would allow me to define my own implementation, but I couldn't.
That's how I'd imagine the implementation
class MyDummyList(list):
def __sum__(self): -> str:
return 'a'
x = MyDummyList()
print(sum(x)) -> # prints 'a'
Obviously this breaks as there is no dunder method __sum__ but I am somewhat surprised that there is no such a way to do that using dunder methods as sum is a quite common operation on iterables.

Use of None and self keywords in method construction [duplicate]

This question already has answers here:
What does -> mean in Python function definitions?
(11 answers)
Python3 function definition, arrow and colon [duplicate]
(3 answers)
What does the -> (dash-greater-than arrow symbol) mean in a Python method signature? [duplicate]
(1 answer)
Closed 9 months ago.
I'm analyzing some old code that I've inherited, and I have a question about the use of "self" and "None" keywords, specifically in the following example:
def run(self) -> None:
I understand that the self keyword is similar to the "this" keyword in C++ in that, in conjunction with the dot operator, it allows us to access the attributes and methods of the class in question. What I'm really interested in is the use of "-> None" in the declaration of the method named "run." Is this in PEP 8 because I can't find an example. I'm using Python 3.7, in case that matters.
What is the purpose of writing a method in this manner? What does "-> None" do?
They're called type hints, and they enable annotating the types of the parameters and return types of functions.
https://peps.python.org/pep-0484/

Python dot notation method attributes VS other methods [duplicate]

This question already has answers here:
Why isn't the 'len' function inherited by dictionaries and lists in Python
(7 answers)
Why does Python code use len() function instead of a length method?
(7 answers)
In Python, when should I use a function instead of a method?
(5 answers)
Difference between len() and .__len__()?
(5 answers)
Closed 4 years ago.
In Python, why are some built-in functions called using brackets with the method name before the object, e.g.
print("foobar")
bool("foobar")
...
While others are built-in method attributes, called with a dot behind the object, e.g.
"foobar".capitalize()
Specifically I'm interested to learn if there is a general principle behind this instead of just common practice and memorization. In cases where you can't quite remember whether it was capitalize("foobar") or "foobar".capitalize(), how do you know?

Accessing data in variables in class Python 3 [duplicate]

This question already has answers here:
Python: access class property from string [duplicate]
(4 answers)
Closed 4 years ago.
I am using Python 3.5 and wish to do something like this
I have created a class which has variables Bitcoin, Monero , 'Etherum' ,etc with various integer values ,I wish to extract them
var1="Bitcoin"
value=classobj.var1 // there is a class which has a variable called Bitcoin and its value is 10 I wish to get its value using classobject.Bitcoin but the variable called var is Dynamic
print (value)
How do I achieve the same ?
EDIT
I know it is possible using switch statement but I am looking for other ways
This is almost always a bad idea—and you really should explain why your design looks like this, because it's probably a bad design.
But "almost always" isn't "always", so Python has a way to do this:
getattr(classobj, var)

What does it mean for a method to be a classmethod and what does parameter * represent? [duplicate]

This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
What is the purpose of class methods?
(18 answers)
Closed 5 years ago.
Looking into the Python documentation, I came across the following page:
https://docs.python.org/3/library/stdtypes.html#int.from_bytes
Here, the third argument of int.from_bytes() is *. What does * represent and why can it be ignored in the examples?
Also, the fromkeys(seq[, value]) method is said to be a classmethod, but what exactly does this mean? What class does this method belong to?

Categories

Resources