<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix"><br>
On 04/26/2013 09:27 AM, Serhiy Storchaka wrote:<br>
</div>
<blockquote cite="mid:kle9t4$53i$1@ger.gmane.org" type="cite">26.04.13
18:50, Larry Hastings написав(ла): <br>
<blockquote type="cite">The standard Java documentation on enums:
<br>
<br>
<a class="moz-txt-link-freetext" href="http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html">http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html</a>
<br>
</blockquote>
<br>
This example requires more than features discussed here. It
requires an enum constructor. <br>
<br>
class Planet(Enum): <br>
MERCURY = Planet(3.303e+23, 2.4397e6) <br>
[...]<br>
This can't work because the name Planet in the class definition is
not defined.<br>
</blockquote>
<br>
It can't work because you inserted the word "Planet" there. If you
omit the word "Planet", this would work fine with something like the
metaclass instantiate-all-data-members behavior in flufl.enum 4.<br>
<br>
Here is the hack, demonstrated in Python 3:<br>
<blockquote>class Metaclass(type):<br>
def __new__(cls, name, bases, namespace):<br>
result = type.__new__(cls, name, bases, dict(namespace))<br>
for name, value in namespace.items():<br>
if not (callable(value) or name.startswith("__")):<br>
value = result(name, value)<br>
setattr(result, name, value)<br>
return result<br>
<br>
class Planet(metaclass=Metaclass):<br>
MERCURY = (3.303e+23, 2.4397e6) <br>
<br>
def __init__(self, name, value):<br>
self.mass, self.radius = value<br>
<br>
def surfaceGravity(self):<br>
return 6.67300E-11 * self.mass / (self.radius ** 2)<br>
<br>
def surfaceWeight(self, otherMass):<br>
return otherMass * self.surfaceGravity()<br>
<br>
<br>
print("If you weigh 175 pounds, on Mercury you'd weigh",
Planet.MERCURY.surfaceWeight(175))<br>
</blockquote>
<br>
<br>
<i>/arry</i><br>
</body>
</html>