__init__.py Using it as an inherited object.

92 views
Skip to first unread message

Jeremy YeoKhoo

unread,
May 11, 2014, 5:51:36 AM5/11/14
to python_in...@googlegroups.com
Hey gu
I'm starting to cleanup my code, and since I've self taught programming from the get go and arent aware of some basic things. I have a question,. In regards to an __init__.py file. Can I use a folder that contains a __init__.py as an inherited object? And if I can, how do i structure the code?


So if Car folder has __init__ how do I the call:

class sportscar(car):
    def __init__(self, car.__init__):
        print "go vroom"


Marcus Ottosson

unread,
May 11, 2014, 5:59:38 AM5/11/14
to python_in...@googlegroups.com

You’d be rather unique in doing that and I wouldn’t consider it very Pythonic, making it an extra barrier for readers of your code to grasp.

Instead, you could define a class in your __init__.py and inherit from that.

Folders with __init__.py files are called “Packages” and can be treated much like a class.

import mypackage
mypackage.MyClass()

class MyNewClass(mypackage.MyClass):
    pass

This would be called working with namespaces, which is very Pythonic.



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f36c7b49-d6dd-41c0-aa5f-4cc31a581034%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
May 11, 2014, 7:56:49 AM5/11/14
to python_in...@googlegroups.com
I would say though that some might find it bad form to put class definitions and extensive code into the __init__.py of the package, as really what it is meant for is initialization. Personally I feel it is more appropriate to do this:

mypackage/
    __init__.py
    myclass.py

__init__.py

from myclass import MyClass
__all__ = ["MyClass"]

If you end up with a package (directory) with just an __init__.py, then what you really wanted was a single module file. 
The kinds of things I put in my __init__.py (if they have anything at all) are import that aggregate subpackages, or initialization code that has to take the current platform into consideration. Basically anything that should set up the package. 




Marcus Ottosson

unread,
May 11, 2014, 10:44:27 AM5/11/14
to python_in...@googlegroups.com
I agree.

I stick with empty __init__.py files, unless there is very good reason for not doing so. I would google about it and gain some perspective. There are reasons to use and not to use in both personal style all the way to performance.

For more options, visit https://groups.google.com/d/optout.


--
Marcus Ottosson
konstr...@gmail.com


Jeremy YeoKhoo

unread,
May 11, 2014, 7:57:37 PM5/11/14
to python_in...@googlegroups.com
Thanks again for the responses. I dont usually put anything in the __init__.py files, but I noticed that in some instances for some python packages elsewhere that this has been done. My issue is having repetitive names when calling a module.

Say for instance my package is:

vehicle/
    __init__.py
    car.py
    sportscar.py
Where inside car.py, I will have 

Class CAR():
   
def __init__(self, drive):
       
if drive:
           
self.engine(True)
   
def engine(self, turnOn= False):  
       
print "Im a lawnmower. :("
       
if turnOn:
           
print "putt putt"
   
def chassis(self):
       
print "basic"
   


And inside sportscar.py

from vehicle import car as car

class sportscar(car.CAR):
   
def __init__(self, drive):
       car
.CAR.__init__(drive)
   
def engine(self, turnOn):
       
print "I am now a V12"
       
if turnOn:
           
print "Hell yes"
       

SO my issue is having to parse in car.CAR, seems repetitive. But I guess the fact that not many people go through this.... I must be approaching structure of my code incorrectly.

How would you guys approach this?

-Jeremy

Justin Israel

unread,
May 11, 2014, 9:20:09 PM5/11/14
to python_in...@googlegroups.com
Is the Car class an abstract or concrete class? You could name it something like:

base.Car
car.AbstractCar
car.CarBase

I wouldn't be too concerned with seeing something like car.Car because it is possible you might put other things into that module and it just ends up being a car namespace for a few things. You also could do  "from car import Car" in your __init__.py and then you can access that base class from your entire package like "from myPackage import Car"




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Jeremy YeoKhoo

unread,
May 13, 2014, 3:30:23 AM5/13/14
to python_in...@googlegroups.com
Thanks Justin,

I guess if I have Car.car it could be an indication on how I designed/approached my code even if it is a question of semantics.

Thanks

-Jeremy

On Sunday, 11 May 2014 19:51:36 UTC+10, Jeremy YeoKhoo wrote:

Marcus Ottosson

unread,
May 13, 2014, 4:22:16 AM5/13/14
to python_in...@googlegroups.com
Hey Jeremy,

It's a good idea to stick with Python conventions in your code. It will in general make it easier for others to read your code, and for you to read others.

Classes should use the MixedCase naming convention, see here: PEP8. Also, files and folders should all be lower-case, so as to eliminate cross-platform issues. E.g. Linux differentiates between Car.py and car.py, whereas Windows does not, resulting in issues when both files are available via a samba-network for instance.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Jeremy YeoKhoo

unread,
May 13, 2014, 8:58:53 PM5/13/14
to python_in...@googlegroups.com
Thanks Marcus, I have been recently acquainted with PEP8 and il have a look at it again :)




 
On Sunday, 11 May 2014 19:51:36 UTC+10, Jeremy YeoKhoo wrote:

Marcus Ottosson

unread,
May 14, 2014, 1:23:46 AM5/14/14
to python_in...@googlegroups.com
You're welcome. :) Good luck!


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages