Constructor call in the same class?

Thomas Jollans thomas at jollans.com
Sun Jul 25 11:53:01 EDT 2010


On 07/25/2010 05:41 PM, Karsten Wutzke wrote:
> What's wrong with:
> 
> class Enum(RootFragment):
>     __jpaTypes = {
>         # complete!
>         'CascadeType': Enum("javax.persistence.CascadeType"),
>         'DiscriminatorType':
> Enum("javax.persistence.DiscriminatorType"),
>         'EnumType': Enum("javax.persistence.EnumType"),
>         'FetchType': Enum("javax.persistence.FetchType"),
>         'FlushModeType': Enum("javax.persistence.FlushModeType"),
>         'GenerationType': Enum("javax.persistence.GenerationType"),
>         'InheritanceType': Enum("javax.persistence.InheritanceType"),
>         'LockModeType': Enum("javax.persistence.LockModeType"),
>         'PersistenceContextType':
> Enum("javax.persistence.PersistenceContextType"),
>         'TemporalType': Enum("javax.persistence.TemporalType"),
>     }
> 
>     # constructor
>     def __init__(self, package, modifiers, name, superInterfaces = [],
>                  annotations = [], innerClasses = [], properties = [],
> methods = []):
>         RootFragment.__init__(self, packageName, modifiers, "enum",
> name, superInterfaces, annotations, innerClasses, properties, methods)
> 
> 
> ?
> 
> I get
> 
>     'CascadeType': Enum("javax.persistence.CascadeType"),
> 
> NameError: name 'Enum' is not defined

well, within the class statement, it's not defined. So you can't call
Enum yet.

You have to create your dict somewhere else. You can either set it from
outside:

class Enum(RootFragment):
    ...

Enum._jpaTypes = { ... }


Or, do exactly the same thing, but within a class method:

class Enum(bla):
    @classmethod
    def contruct_jpatypes(cls):
        cls.__jpaTypes = { ... }

Enum.construct_jpatypes()

> 
> What's wrong with calling a constructor in a dict initializer? How do
> I solve this?
> 
> Karsten




More information about the Python-list mailing list