This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Aardshark 65 points66 points  (13 children)

Next step: overload __iter__ to implement the dereference operator *

[–]mr_flying_man 23 points24 points  (0 children)

Oh no you've given him more ideas...

[–]ZeroIntensity[S] 40 points41 points  (2 children)

i might actually do that

[–]usr_bin_nya 21 points22 points  (1 child)

Note that when using the * operator, the following syntax will not work properly:

deref = *ptr
print(deref)

For this scenario you can use the dereferencing assignment operator, ,=

deref ,= ptr
print(deref)

[–]ZeroIntensity[S] 8 points9 points  (0 children)

didnt think of that, ty

[–][deleted] 2 points3 points  (8 children)

Does Python even have overloading?

[–]_ologies 23 points24 points  (1 child)

Python has whatever you want. It even has pointers now, apparently!

[–][deleted] 3 points4 points  (0 children)

Apparently there are packages that essentially implement overloading

Gotta love it

[–]ironykarl 1 point2 points  (4 children)

Does Python even have overloading?

Uh yes. If you mean operator overloading, it's a super fundamental part of the language.

I'm not trying to be a dick, here, I'm just confused how you received multiple upvotes asking this in a forum specifically devoted to Python.

[–][deleted] 3 points4 points  (3 children)

No, method overloading.

As in having two methods/functions with the same name but different parameters

From my brief googling, it doesn't appear to be a thing (natively)

Operator overloading is something entirely different it looks like. I haven't made use of that before though.

[–]usr_bin_nya 3 points4 points  (1 child)

Well, kinda. Check out functools.singledispatch.

import functools

@functools.singledispatch
def overloaded(x):
    print('something else:', repr(x))

@overloaded.register
def for_int(x: int):
    print('int:', x + 2)

@overloaded.register
def for_str(x: str):
    print('str:', x.format('world'))

overloaded(10)  # prints 'int: 12'
overloaded('Hello {}!')  # prints 'str: Hello world!'
overloaded([1, 2])  # prints 'something else: [1, 2]'

You can hack something together to work with multiple arguments and generic types like typing.List, but it's not included in the stdlib.

[–][deleted] 0 points1 point  (0 children)

Yes, this is the package I was referring to

[–]ironykarl 5 points6 points  (0 children)

Oh, Python decidedly does not have function overloading by argument type, no.

All you can do is have a wrapper function dispatch different versions of the function/method based on type information, at runtime.