-2

Question says it all.

F.e. when you refer to an object with a class of the bge.types-module which would then read like bge.types.class.

Thilo
  • 257,207
  • 101
  • 511
  • 656
Jonathan
  • 101
  • 3
  • Question does not say it all... Please elaborate a bit more. Also, is this about a specific language? – Thilo Aug 14 '15 at 08:26
  • What does that even mean? What language? For example, in `Java`, `Object.getClass` returns a `Class>`. `m` is an *instance* of a class in `MyClass m = new MyClass()`. – M. Shaw Aug 14 '15 at 08:27
  • Lol I wrote it wrong sorry. Language is Python I thought it was posted in it. – Jonathan Aug 14 '15 at 08:30

2 Answers2

0

No, modules are not classes. The class construct is essentially used to define a type, and you can instantiate types by calling them. You can not however call modules, and you cannot instantiate them at all.

You can think about modules as singletons: There can only ever exist a single instance of a module.

Modules can contain members that are types though, so essentially modules can contain classes.

poke
  • 369,085
  • 72
  • 557
  • 602
  • You can instantiate them: `types.ModuleType('foo')` – Duncan Aug 14 '15 at 08:51
  • @Duncan That creates a new module though. Modules themselves are not types, and you cannot instantiate them. If you import e.g. `sys`, you cannot instantiate `sys`. So no, modules are not types/classes just like actual int objects are not types/classes. – poke Aug 14 '15 at 09:00
  • Thanks a lot guys! I see there is a little discussion over this topic, glad I asked it :) Btw I just read about metaclasses this is were this question becomes especially interesting. – Jonathan Aug 14 '15 at 09:12
  • @Jonathan For meta classes, check [this excellent answer](http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python/6581949#6581949) on the topic. – poke Aug 14 '15 at 09:15
  • @Jonathan, the discussion is more about the meaning of words. I completely agree with poke that you cannot create an instance from an existing module because a module is an instance not a type, but I was pointing out that you can create instantiate a module which is one interpretation of what he said was impossible. tl;dr: You need to be very precise to avoid ambiguity when discussing these issues. – Duncan Aug 14 '15 at 09:21
  • @Duncan Nvm I also didnt know that you can instantiate a module like this. – Jonathan Aug 14 '15 at 09:26
0

Surprisingly, although the module type itself is defined in C, it is a type just like any other and you can even subclass from it:

>>> import types
>>> types.ModuleType
<class 'module'>
>>> class C(types.ModuleType):
    def foo(self):
        print("hello")


>>> my_module = C('my_module')
>>> my_module
<module 'my_module'>
>>> 

Uses for this are strictly limited though I did for fun once write some code that would replace a class in sys.modules with a subclass instance that was a copy of the original module but with support for getattr.

In normal use there is precisely one module type: individual modules are instances of the module type not separate types in their own right.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • This essentially says that the module type is a type. The question asked whether modules themselves are classes though.. – poke Aug 14 '15 at 09:08
  • Thanks a lot guys! I see there is a little discussion over this topic, glad I asked it :) Btw I just read about metaclasses this is were this question becomes especially interesting. – Jonathan Aug 14 '15 at 09:12