import issue with classes
I have a class FooB that derives from FooA i.e. class FooB(FooA): FooA.__init__(self,...) Can someone explain why Import FooA doesn't work and I need to use from FooA import FooA instead? This puzzles me. Thanks. -- [ Rodrick R. Brown ] http://www.linkedin.com/in/rodrickbrown -- http://mail.python.org/mailman/listinfo/python-list
Re: import issue with classes
On 1/3/2012 8:50 PM, Rodrick Brown wrote: > Import FooA doesn't work and I need to use from FooA import FooA > instead? This puzzles me. > Thanks. If you have a module called FooA with a class called FooA, then import FooA imports the /module/. The class would be FooA.FooA, just as the variable x from FooA would be FooA.x. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 -- http://mail.python.org/mailman/listinfo/python-list
Re: import issue with classes
On 1/3/2012 10:06 PM, Andrew Berg wrote: On 1/3/2012 8:50 PM, Rodrick Brown wrote: Import FooA doesn't work and I need to use from FooA import FooA instead? This puzzles me. Thanks. If you have a module called FooA with a class called FooA, then import FooA imports the /module/. The class would be FooA.FooA, just as the variable x from FooA would be FooA.x. This sort of confusion is why it is not recommended to have file/module fooa contain class FooA and why there were some module name changes in Py 3. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: import issue with classes
On Tue, Jan 3, 2012 at 9:50 PM, Rodrick Brown wrote: > I have a class FooB that derives from FooA > > i.e. > class FooB(FooA): > FooA.__init__(self,...) > > Can someone explain why > > Import FooA doesn't work and I need to use from FooA import FooA instead? > This puzzles me. > Thanks. > > > > -- > [ Rodrick R. Brown ] Because you're coming from Java and thinking that Python behaves the same way, which isn't true. In Java, every code file consists of a single public class with the same name as the file. In Python, a module is an object consisting of many other objects, including classes, functions, and variables. There's no need for the class name to be anything related to the file name. By convention, modules (files) have lowercase names. So lets call your file "fooA.py" with a class FooA inside it, just so we can distinguish between them. "import fooA" searches for a file called fooA.py in a set of directories, finds it, compiles it, and binds the now-created module object to the name fooA. The module object fooA has a class "FooA" inside it. They're two separate things. "from fooA import FooA" searches for fooA.py, just like before. But instead of giving you the whole module, it just looks for FooA and binds it to the same name in the current context. -- http://mail.python.org/mailman/listinfo/python-list
Re: import issue with classes
On 1/3/2012 11:13 PM, Terry Reedy wrote: On 1/3/2012 10:06 PM, Andrew Berg wrote: On 1/3/2012 8:50 PM, Rodrick Brown wrote: Import FooA doesn't work and I need to use from FooA import FooA instead? This puzzles me. Thanks. If you have a module called FooA with a class called FooA, then import FooA imports the /module/. The class would be FooA.FooA, just as the variable x from FooA would be FooA.x. This sort of confusion is why it is not recommended to have file/module why it *is* recommended (to have different names) and not recommended to have the same name, including case. fooa contain class FooA and why there were some module name changes in Py 3. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list