Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Implementation of the algorithm in Wiki article has a drawback.
If the tree consists from only one root then nothing happen. The tree is not
modified. How to fix this issue? In C++ it is possible to set root pointer to null, but what to do in python?
This is a continuation of a previous question, and the OP is looking to replace the value with None, but for all names that have that instance assigned to them - this is not possible in Python.
The answer is there is no way to delete the value in this case due to the way Python is designed - you would have to implement an object manager of some kind, store it in a container and access it through that, or change your design not to rely on mutation.
Another question worth asking - is this a case that is likely to happen? It may be possible (and desirable) just to define this as a limitation where the root node can't be destroyed. I can't think of a case where you would want that functionality.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I created a class.
After that i instantiated that class by the variable name 'xyz'.
Now is there a way that I can display the name of this variable i.e. print 'xyz' inside __init__in the class??
Like #Patrick Haugh mentioned in the comments, since the right hand side of the assignment is always executed first and then the assignment is evaluated with the result of that execution there is no (easy) way for the right hand side to know what the variable name would be.
Although not good, if you absolutely need to know this piece of information you can always pass the name as a parameter...
xyz = MyClass('xyz')
I believe there is a way of solving what you are asking with Metaprogramming, but since I have not experience in that area I wouldn't dare to give you any advice
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a module which only contains functions. Is it good practice to put these inside a class, even if there are no class arguments and the __init__ function is pointless? And if so how should I write it?
There's no particular reason to force functions to go inside a class if there's no reason to make a class. A python sourcefile already provide a perfectly reasonable namespace for storing similar, related functions. The pypy implementation of the python programming language, for example, has many modules that don't have classes in them (for example, time).
You would want a certain data structure as a class, of course, but there are certain behaviors that are entirely independent of data structures, and putting them in a class would just add needless complexity.
It is good to build modules that contain a class for better organization and manipulation depending on how big the code is and how it will be used, but yes it is good to get use to building classes with methods in them. Can you post your code?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
please I want to know a way to remember or to easy difference between using
v.function() or function(v) in python # v is a variable or object
capitalize(c) #error
c.capitalize() # OK
c="hello"
c.capitalize()
The first version calls a function with v as an argument, the second one calls a method of the object v. Which one it is in a particular case may vary depending on the design approach, but in general:
1) If it modifies the state of an object, it's likely a method.
Examples: list.sort(), dict.pop().
2) If it works on a variety of similar types, e.g. on containers, it's likely a function.
Examples: len(), sum(), sorted().
3) If it object type specific and has to know something about the object implementation internals, it's likely a method.
Examples: str.capitalize(), str.split().
4) If in doubt - refer to the documentation.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
Improve this question
I have read the topic What do I use for a max-heap implementation in Python? that has been viewed more than 74k times as of today (meaning that many people came across the same issue) and I've been wondering what is the reason for not implementing max heap data type in the Python Standard Library? Negating or inverting the values and keeping them in min heap looks to me very ugly and introduces unnecessary overhead (we need to take care of applying the transformation ourselves twice).
Edit: As pointed by #Boris this issue has been already raised in enhancement request 27295 and rejected, in 2016. Here is Raymond Hettinger's justification:
Sorry James, we don't grow the APIs without sufficient demonstrated need; otherwise, we end-up with API sprawl. People with actual use cases haven't requested behavior (and the occasional one-off gets by negating the numeric argument). That is why the maxheap functions were intentionally made private.
FWIW, this module is very old and the core functions have long proven themselves sufficient to meet their use cases (like being used in an event loop to efficiently select the next scheduled event).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a python object that represents a connection to a rest api with a large number of endpoints. I need to come up with a strategy to organize hundreds of method calls (right now I am using prefixes on the methods). I would also like to break up the files if that's possible.
I am working in python 2.7
Two give more clear questions -
1) Is it possible to split an object into multiple files in some sort of effective manner in python? Specifically without introducing more complexity?
2) Is it possible to make "submodules" for lack of a better phrase, so
my_rest_api.users.create
rather than
my_rest_api.users_create
You can implement the groups as mixins. For example like this:
user_actions.py
class UserActions:
def user_create(...): ...
the_api.py
from . import user_actions
class TheApi(UserActions, ItemActions, OtherActions, ...):
# common functionality goes here
Then you can call the_api_instance.user_create(...) once you have an object created.