Tag Info

New answers tagged

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.


0

The reason why you are not getting anything back is because the append method has no return value. You could do: A=['1'] C=A C.append('1') print(C) Then you should get the right answer for your case that you are expecting to get.


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 ...


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.


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 ...


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 ...


0

No, it would be worse functions should tell what they do. With a good naming, your check_stuff function indicates what it does. It should therefore be named e.g. checkConditionsValid depending on what it actually does. I suppose your function func also returns something or has some side effect in case that the conditions are. If not, that would be a design ...


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 ...


0

... when it is installed on a new computer, it installs the database (mysql or sqlite etc) without the need to first install database? Yes, but I would suggest, don't. Install the database separately (and only once) on a known machine. Then provide the name/ I.P.Address of that machine when installing all of the client applications. If you don't do ...


0

Rather than parsing C# code, my suggestion would be to store the error codes and their associated descriptions in external resource files (XML, JSON, CSV, database table, whatever). That way all the Python code has to do is look up the code and retrieve the human-readable description. This will also allow you to add localization/translation support or update ...


-3

In a short answer YES. But, here is why HTML is the structure of all web and JS is the interaction of the web. I know you probably heard that a lot but, it is very important to know. Some frameworks will let you not use (or not use a lot) of HTML and JS but, they are the basics of the web. If you do not want to learn code hire someone. And also I know this ...


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 ...


0

I think the simplest, most Pythonic way to implement a singleton class is with inheritance: class Singleton(object): _instances = {} def __new__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = object.__new__(cls, *args, **kwargs) return cls._instances[cls] class Foo(Singleton): # Foo 'is a' ...


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 ...


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 ...


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 ...


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 ...


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, ...


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 ...


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 ...


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

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 ...


0

you might want to checkout column-oriented databases. I am not sure what you mean by array databases but with my suggested approach you can have a dynamic number of values per time frame. You can also have multiple values for the same timestamp. The interesting part is that if you have values measured at the same timestamp you can save them as additional ...



Top 50 recent answers are included