Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Split class across multiple files

Reply
Thread Tools

Split class across multiple files

 
 
eric.frederich
Guest
Posts: n/a
 
      11-20-2009
I have a class which holds a connection to a server and a bunch of
services.
In this class I have methods that need to work with that connection
and services.

Right now there are about 50 methods some of which can be quite long.
From an organizational standpoint, I'd like to have method
implementations in their own files.

Is this possible? It is recommended? Should I just stop worrying
about it and have a 5,000 line class?
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-20-2009
eric.frederich schrieb:
> I have a class which holds a connection to a server and a bunch of
> services.
> In this class I have methods that need to work with that connection
> and services.
>
> Right now there are about 50 methods some of which can be quite long.
> From an organizational standpoint, I'd like to have method
> implementations in their own files.
>
> Is this possible? It is recommended? Should I just stop worrying
> about it and have a 5,000 line class?


It is pretty easy to do, as python allows multiple inheritance. Just
group together methods into a class, and create one that inherits from all.

However, I'd say something that even splitted up is essentially one
class with 5000 lines cries for a huge refactoring. Of course this
depends on the use-case, but I for once don't have a single such beast.
Looks like a god-object to me...

http://en.wikipedia.org/wiki/God_object

Diez
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      11-20-2009
On Nov 20, 8:14*am, "eric.frederich" <(E-Mail Removed)> wrote:
> I have a class which holds a connection to a server and a bunch of
> services.
> In this class I have methods that need to work with that connection
> and services.
>
> Right now there are about 50 methods some of which can be quite long.
> From an organizational standpoint, I'd like to have method
> implementations in their own files.
>
> Is this possible?


Yes, define all your different methods in separate classes in separate
files. Then subclasses all of those separate classes into one big
class.


> It is recommended?


Debateable. Personally, I have no problem using inheritance for
purely mechanical reasons. Others might. (In my case, it was a
separation into general and customized behavior. I wanted to define
generic behavior in one package, and specialized behavior in another,
but there was no internal logical difference between specialized and
generic methods.) However....


> Should I just stop worrying
> about it and have a 5,000 line class?


Five thousand lines is pushing the comfort level of how big a class
ought to be. I suggest instead of worrying about how to mechanically
split off methods into different files, you should consider whether
there's a way to refactor that one big class into smaller ones. Then
the problem you have goes away.


Carl Banks
 
Reply With Quote
 
eric.frederich
Guest
Posts: n/a
 
      11-20-2009
Yeah... it does seem like a God Object.
My reasoning for putting them all into one big class is that the class
has references to all the required resources, connections, and
services.
The other option would be to have a simple object called Session which
did nothing but login and hold those references.
The problem then is that I'd have 50 methods all of which would need
to take that session object as an argument.
That is what I was trying to avoid, having a ton of methods all taking
the same first parameter.
In most cases there would only be one session.

Perhaps I could do a singleton approach and have each of these methods
implicitly use the single session object unless called with a session=
keyword argument.

How does this look?.....


# Begin Session.py

class Session():

def __init__(self, host=None, username=None, password=None):
if host and username and password:
self.login(host, username, password)

def login(self, host, username, password):
self.host = host
self.username = username
self.password = password

self.connection = Something.login(host, username,
password)
self.someService = SomeService.getService(connection)
self.anotherService = AnotherService.getService(connection)

def logout(self):
return self.connection.logout()


defaultSession = Session()


# Begin MyLibrary.py

from session import defaultSession

def doSomethig(x, y, z, session=defaultSession):

return session.someService.doSomething(x, y, z)

def anotherFunction(x, y, session=defaultSession):
ret = 0
for item in session.anotherService.getStuff(x):
if session.someService.foo(item, y):
session.anotherService.bar(item, x)
ret += 1
return ret





On Nov 20, 11:31*am, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> eric.frederich schrieb:
>
> > I have a class which holds a connection to a server and a bunch of
> > services.
> > In this class I have methods that need to work with that connection
> > and services.

>
> > Right now there are about 50 methods some of which can be quite long.
> > From an organizational standpoint, I'd like to have method
> > implementations in their own files.

>
> > Is this possible? *It is recommended? *Should I just stop worrying
> > about it and have a 5,000 line class?

>
> It is pretty easy to do, as python allows multiple inheritance. Just
> group together methods into a class, and create one that inherits from all.
>
> However, I'd say something that even splitted up is essentially one
> class with 5000 lines cries for a huge refactoring. Of course this
> depends on the use-case, but I for once don't have a single such beast.
> Looks like a god-object to me...
>
> http://en.wikipedia.org/wiki/God_object
>
> Diez


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      11-20-2009
eric.frederich wrote:
> I have a class which holds a connection to a server and a bunch of
> services.
> In this class I have methods that need to work with that connection
> and services.
>
> Right now there are about 50 methods some of which can be quite long.
>>From an organizational standpoint, I'd like to have method

> implementations in their own files.
>
> Is this possible?


A method is just function that is a class attribute.


from somemethod import somemethod # has def somemethod

class Huge:
somemethod = somemethod

# or

class Huge:
defs
...

Huge.somemethod = somemethod

> It is recommended? Should I just stop worrying
> about it and have a 5,000 line class?


I suspect some refactoring would be useful.

 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      11-22-2009
eric.frederich wrote:
> I have a class which holds a connection to a server and a bunch of
> services.
> In this class I have methods that need to work with that connection
> and services.
>
> Right now there are about 50 methods some of which can be quite long.
> From an organizational standpoint, I'd like to have method
> implementations in their own files.
>
> Is this possible? It is recommended? Should I just stop worrying
> about it and have a 5,000 line class?


If you're not joking about the 5000 lines estimation, then with 50
methods, each method would be ~100 lines? That stinks of huge refactoring.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Distributing methods of a class across multiple files lh Python 11 01-26-2012 02:41 PM
Split a class across two source files jeremy.figgins@gmail.com C++ 2 12-15-2005 05:30 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Why does split operate over multiple lines in the absence of "ms" ? And why doesn't $_ work with split? Sara Perl Misc 6 04-12-2004 09:07 AM



Advertisments