Python attribute resolution: the ultimate flowchart [closed] - python

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 6 years ago.
Improve this question
I'm looking for a complete description of Python attribute lookup. I know there have been similar questions and there is this very nice introduction to the descriptor protocol. But it does not cover the entire lookup for example it does not elaborate on special methods.
Ideally, there would be a flowchart starting at x.y and then running through all possibilites including
is it lookup or assignment?
is x a class or an object (or for example a function if that makes a difference)?
is x a builtin if that's relevant?
does x have a __slots__ attribute?
does x have a __getattr__, __setattr__ or __getattribute__ method?
is y, the string a special attribute name
once y is resolved, does it have a __get__ attribute?
etc., etc., you get the idea.
I realise this is a lot of work, so if there are any promising submissions within 5 days from now I'm willing to offer a 250 rep bounty.
Reference version should be Python3.6 and there should be an effort made to demonstrate completeness as far as possible.
I'm sure such a flowchart would be tremendously useful for me and many others.

Related

Python instances should be created at the beginning or create them as we need them? [closed]

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 10 months ago.
Improve this question
If we talk about resources, memory, legibility what would be better between:
Create all the instances at the beginning of a method? (left side of the screenshot)
Create the instances as we need to use them? (right side of the screenshot)
Do you have any documentation that says which is better? I searched in the Python documentation but found nothing.
Well in python creating instances is creating instances, regardless of where you do such, the same amount of instances will be created taking the same amount of storage space.
If you're looking into memory conservation I'd recommend using the del operator when instances are no longer needed.
Other than that this is totally just up to personal preference, and how you'd like to format your code.

can someone explain the purpose of the strategy metaclass mentioned here [closed]

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
Cannot link the example to the title, so here it is: Python strategy design pattern example
First let's look at the basic idea of Strategy Pattern. What it really says is developing some algorithm (function/method/code fragment) which can be switched at run time.
If we are using an OOP supported language (Java, Python), most of the time, we can implement the Strategy pattern with the use of run time polymorphism. In your example also it shows how to achieve this exactly in that way.
No need to get confused with the notion of meta class. It's a python specific terminology, which is used to define the class object of Python. This one is a good answer, if you want to know more about meta classes in python.
And in your example, the notion of meta class doesn't quite related with Strategy pattern implementation. It was just used to mark the Strategy class there as an abstract class. You can develop your program even without that part. And it doesn't do any harm to the idea of Strategy pattern implementation.

How are super, pass, yield and other keywords are implemented in Python? [closed]

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 7 years ago.
Improve this question
I was asked this question in a recent interview. And I know super is the way to get the super class, pass has no effect but to close a function or class declaration, yield is used in generator. But I have no idea how they are implemented in Python.
I searched and only get the answer for super: How is super() in Python 3 implemented?, anybody can answer for others?
super is not a keyword in python, it's a variable like int or range.
Keywords like pass, yield, def, if, ... are consumed by the parser to build an AST (Abstract-Syntax-Tree). You can play with the AST by yourself with the ast-module of python.
The AST is then turned in some lower level machine or virtual machine code by the code generator, ready for execution.

python shorten nested unmutable object variable names [closed]

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 8 years ago.
Improve this question
This question is mostly just about curiosity.
As a java programmer myself, I often get frustrated by all the dots on nested variables. (There is kind of a de facto standard in java that any line with a variable with more than 2 dots needs to be re-written). Is there anyway to "rename" a nested variable such that I don't have to type all the dots everytime?
For example
class MyClass():
def my_func():
if not self.app.arguments.foo:
self.app.arguments.foo = 'bar'
print self.app.arguments.foo
Is there a way to write this such that I don't have to type the full name self.app.arguments.foo everytime?
Second, what is the standard 'pythonic' way of using variables as such. Would standard python nomenclature say just use all the dots all the time?
Extra notes
As I said, this is more about curiosity. So lets assume that I do not have access to module b. In other words I can not (more like don't want to) write getter and setter methods.
Also note that the self is important. I know I can do a rename with an import, but you cant import self.
You can use a temporary variable for all but the last item in a chain:
b = self.a.b
if b.c:
b.c = 'foo'
print b.c
If you weren't assigning, you could go all the way to c.
This is a little bit faster as well as being easier to write. However, it may or may not be easier to read.

Superclass inherits from a subclass. Coursera. Are they crazy? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Learn to Program: Crafting Quality Code
Screenshot
This is a video lecture from Coursera Learn to Program Crafting Quality Code Course.
So there is a quiz during the video.
class MyInt(int):
# some code there
They gave 4 answers where I need to choose a right one.
I've choose my answers one by one but finally they say that right is absolutely wrong answer.
int is a subclass of MyInt
This question Python: How do I make a subclass from a superclass? gives me absolutely right confirmation that I'am right.
Where is the truth?
There must be something wrong with the quiz, the code as written in the question is declaring that MyInt is a subclass of int (or equivalently: that int is the superclass of MyInt), no the other way around.
If you inherit from a class, that class is the super-class. In the example int is the super (or base) class and MyInt is the subclass. They're wrong, just as you suspected. ;-)

Categories