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

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

Related

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.

Python attribute resolution: the ultimate flowchart [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 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.

Is it part of clean code to include docstrings in Python? [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 7 years ago.
Improve this question
I am working on a project, and I would like to have my code as clean as I can. I am wondering if including docstrings in a class it is considered as clean code or not. For example, one of my class definitions is as follows:
class Hotel:
"""Class to model the rating and rates of a hotel."""
def __init__(self, rating):
"""
:type rating: int
:param rating: hotel rating
"""
self.rating = rating
Is it correct the way I am writing the code, i.e., am I following the clean code standards.
Thanks in advance.
Clean code is partly a matter of opinion. But most of the ones I'm aware of for Python favor including class and method docstrings. For instance, the Google Python Style Guide says:
A function must have a docstring, unless it meets all of the following
criteria:
not externally visible
very short
obvious
...
That said, there's more to good docstrings than just having them; a good place to start is by asking yourself, what would I need to use this class (or function) if I had never seen it before?

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.

is the common practice in python to put __init__ in the beginning of class definition? [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
I wonder whether the recommended practice is to put init in the beginning of class definition or to put interface functions first:
class examp1:
def ifmethod1(self):
pass
def ifmethod2(self):
pass
def __init__(self):
pass
VS.
class examp1:
def __init__(self):
pass
def ifmethod1(self):
pass
def ifmethod2(self):
pass
A lot of people put it first, but every one can have their own opinion and you'll find arguments for both.
To me, being first is simply useful because it's helpful to know __init__ quickly.
In an effort to help coders of other languages grasp Python quickly, ibiblio.org says "__init__ is analogous to a constructor". In that spirit, it would often be put first — constructors in other languages are almost always listed first.
In one example, the Google style guide for Python, it is always listed first.

Categories