Tag Info

Hot answers tagged

14

Wrt abstraction, I see that repeat(square, 2) returns implementation detail in the form of apply_n_times(n - 1)(f(x)) multiple times before providing the actual result. The function returned by repeat(square, 2) is not an implementation detail; it's the whole point of calling repeat. An implementation detail is something that the caller doesn't need to ...


6

The benefit of abstraction is that the caller doesn't have to know about implementation details. If I understand correctly, you're questioning this python construct because the caller can find about about implementation details. That's not the same thing. Not having to know about how a method does its job is useful. It aids the software developer in their ...


5

Yes, a function that just raises an exception is OK. You can easily have functions like ensureWellFormed(email_address) or http_request.ensureUserLoggedIn() that raise well-documented exceptions. This de-clutters code, and many high-profile projects, e.g. Django, do use this style here and there. If you use this approach, make sure your exception objects ...


4

The caller doesn't so much need to know the implementation details of repeat(f, n) as he needs to know its return value. In this case, the return value is a little more complicated than most, because it returns a function. In a static language like Haskell, this is easily documented in the function signature like: repeat :: (a -> a) -> Int -> (a ...


4

Changing the state of a file handle is such a heavy-weight side-effect that I would want it heavily documented in the interface precisely what the method does, and then it doesn't matter which way you decide.


4

You need self when you want to refer to the instance of a class within the class itself. For example, an instance method can call other instance methods or properties or use instance fields. If you have used other object oriented languages, you may have a habit of using this. Python's self is similar (while not identical) to this in other languages. The ...


4

The main drawback of generators is they can only be traversed in one direction. There's no going back to a previous value. You also can't share them. There are many cases where that can easily be accounted for, or even where it is preferable, but there are also many cases where it isn't. Sorting, for example. That's why a lot of times you'll see ...


3

An object-oriented programmer's instinct on this sort of problem is that at some point they'll have to instantiate some sort of Node object, but you really don't. You don't even need to create a 2D array. The path-finding algorithms are described largely in terms of sets. You can write very clear implementations by parsing your maze into sets of (row, ...


2

A namespace is a hash of name, value pairs, a lot like a simplified Python dictionary. When you do an assignment, like a = 1, you're mutating a namespace. When you make a reference, like print a, Python looks in one or more namespaces to find the object with that name, a. A scope defines which namespaces will be looked in and in what order. A namespace ...


2

There is an excellent article on Python namespaces here. To quote the relevant part to answer your question about the reference between scopes and namespaces: A scope refers to a region of a program from where a namespace can be accessed without a prefix. For example, imagine a simple die-rolling program: import random # 'random' is in module ...


2

Whenever someone says 'Circular Dependency' I shudder. I see as an issue on the design. DI is independent of circular dependency. You are indeed creating coupling between the two classes. Is it needed? As a general case, I don't believe so. I think that you are giving too much responsibility to the Person class. Therefore, you are hurting the design. Can ...


2

You've got iterators confused with generators. Your first example is a list iterator expression while the second is a generator expression. The key difference is that the generator creates each member of the given collection lazily (as needed) rather than eagerly (at once, whether needed or not). You can define your own generators by using yield rather than ...


1

In Python, the append method mutates the list it was called on but does not return it. There's not much reason to either, since you already have a reference to the list.


1

Maybe It can work to externalize validation logic as long as it is not externally visible and is used to enforce preconditions. Say you have an object that operates on a database connection, and most of its functions require that the connection be set up and initialized. Rather than checking the connection and conditionally throwing an exception in each ...


1

As indicated by the python docs: classmethod date.fromtimestamp(timestamp) Return the local date corresponding to the POSIX timestamp, such as is returned by time.time(). This may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from ...


1

Looking at it from the perspective of other languages, in both Java and C++ many libraries and interfaces accept a generic InputStream/istream as a parameter when they need to read something (and then to read a file, you'd pass FileInputStream/ifstream). Aside from the benefit of being able to read from other input sources than files, input streams are ...


1

In most cases, it is expected that any function that acts on a file will move the file pointer. There are rare cases where I have used functions to peek ahead in the file, which where expected to leave the file pointer as it was found. In all other cases, I expect the file pointer to move as a side effect of the function. Functions that do not move the ...


1

Generally, it's a good idea to split something into a function of it improves the readability of your code, Or, if the part being oft repeated is somehow core to the system. In the above example, If you needed to greet your users depending on the locale, then it would make sense to have a separate greeter function.


1

In my opinion using regular imports improves readability. When reviewing Python code I like seeing where the given function or class comes from right where it is used. It saves me from scrolling to the top of the module to get that info. As for the long module names I just use the as keyword and give them short aliases: import collections as col import ...



Only top voted, non community-wiki answers of a minimum length are eligible