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
Related
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 last year.
Improve this question
I’m creating a text based adventure game in Python and for the the state of character and amount of one item I’m using global variables, but I’ve heard it’s not the best solution and I can’t seem to find one solid answer.
You have heard correctly. Global variables should always be avoided.
For the purposes of your project, I recommend you look into Object Orientated Programming (OOP) in Python. This is a better way of keeping track of the state of objects in your program.
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 3 years ago.
Improve this question
I was wondering if there's any Python code I can run that, assuming the Python code has not been tampered with, will return an ID specific to that computer that cannot be spoofed. My initial thought was to use MAC addresses, but I know those can be easily spoofed. How about CPU serial number? How would I go about doing that?
Thanks.
This is an impossible problem, as it's equivalent to effective DRM. Every identifier can be spoofed. Remember, the user can tamper with your Python code (any compiling/obfuscating/encrypting you do can be reversed, so don't bother) to return whatever identifier they want. (And even if your code were absolutely read-only, they could change the Python runtime or the OS to do whatever they want.)
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 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 4 years ago.
Improve this question
Which name is given a function that is separated by undescore.
function_name.
And to a function like FunctionName? and what would be the right one for python and ANSI C.??
Function names like func_one are written in snake case, while functions written like FunctionOne are written in Pascal case, which is a subset of Camel case where the first letter is also uppercase.
Thanks to #abelenky for pointing out the initial error.
function_name is called a Snake Case. It is the recommended casing for Python.
See this answer for more enlightenment.
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.