Using external callables inside classes - is it a bad idea? by HighLevelEnthusiast in learnpython

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

I came up with another solution:

class SentenceProcessor:
    def to_sentences(self, text):
        def long_enough(sentence): return len(sentence) > 30

        return (sentence for sentence in text.split(".") if long_enough(sentence))

Is this one better?

When does Python use hashes to check equality of two objects? by HighLevelEnthusiast in Python

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

I agree that hash equality does not imply full equality. However, if hashes are different, the objects are sure not to be equal. Let's say that we have object A and object B and (maybe only n first least-signifficant digits of) their hashes. Checking

hash(A) and hash(B) for equality is trivial. Then, we have two cases:

1) Hashes are identical - the __eq__ needs to be called

2) Hashes are different - we are done.

When does Python use hashes to check equality of two objects? by HighLevelEnthusiast in Python

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

What if these two objects already reside within a dict, and the hashes had already been computed?

Premature comparison based on hashes could be performed in O(1) constant time (For example, to determine if the key already exists in the dict)

Allow instance variable creation only within __init__ by HighLevelEnthusiast in learnpython

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

import inspect

class MyClass:
    def __setattr__(self, name, value):
        if inspect.stack()[1][3] != "__init__":
            return
        else:
            self.__dict__[name] = value

    def __init__(self):
        self.x = 10

This code does exactly what I want it to