6
In languages like PHP and Java, there are interfaces
, which, when implemented in a class, requires it to contain the methods of this interface in the same way as they were declared.
Example in PHP:
<?php
interace UserInterface
{
public function getName();
}
class User implements UserInterface
{
// Se eu adiconar um parâmetro, vai gerar um erro
public function getName()
{
return $this->name;
}
}
class Scholl
{
protected $users = array();
/*
Obriga a implementação de uma classe que implemente a
interface UserInterface
*/
public function addUser(UserInterface $user)
{
$this->users[] = $user;
}
}
In Python, there is an interface or there is some standard for this?
If there are no interfaces, there is some way to "force" a method to exist in a class?
There is a type induction in Python?
The solution does not work in Python 3.
– João Paulo
@Joãopaulooliveirafernandes on your first question (which you apparently deleted) yes, up an Exception, here’s an example http://pastebin.com/FeVZhAB7 but note that this code is written for python2
– drgarcia1986
Yes, I deleted because it didn’t make sense since I tested it in Python 3. Here’s why it doesn’t work in 3 and a base to work: http://stackoverflow.com/questions/18513821/python-metaclass-understanding-the-with-metaclass/18513858#18513858
– João Paulo
It’s simple, just change the basis of the class
Shape
for this syntaxShape(metaclass=abc.ABCMeta)
, for example http://pastebin.com/naYJc0iq– drgarcia1986
Still not pointing out any exceptions
– João Paulo
Did you see the pastbin I sent? The first I drove in Python 2.7 and the second Rodei in Python 3.4, both went up exception...
– drgarcia1986
Yes. Now it’s really good! You could edit your answer. Your solution is faster than @Pablo Palaces. UP!
– João Paulo
Cool.. I’ll include the shape of py3
– drgarcia1986