Struct usages in Python

Alok Kumar alokkat at gmail.com
Thu May 29 00:00:21 EDT 2008


Thanks to everyone for your help.

I am able to use array of structure (here Event is a class) in the following
manner. But here I am fixing the array index as 4. Is there any easy way to
keep it appending dynamically.

self.event = [Event() for x in range(4)]  # Event is a class as posted in
original message.

I have one set of structures and want to process them, subsequently couple
of them will not be able to pass the criteria and all passed ones I wanted
to put in another list where I can dynamically append the structure. So that
I can post this list to server.

Thanks in advance.

Regards
Alok


On Wed, May 28, 2008 at 1:53 PM, Arnaud Delobelle <arnodel at googlemail.com>
wrote:

> Arnaud Delobelle <arnodel at googlemail.com> writes:
>
> > "Alex Gusarov" <alex.m.gusarov at gmail.com> writes:
> >
> >>>  class Event(object):
> >>>
> >>> Always subclass object, unless you have a very compelling reason not
> to,
> >>> or you are subclassing something else.
> >>>
> >>
> >> I've thought that if I write
> >>
> >> class Event:
> >>     pass
> >>
> >> , it'll be subclass of object too, I was wrong?
> >
> > You are wrong for Python 2.X, but right for Python 3 where old-style
> > classes are gone for good.
> >
> > What you define with the statement
> >
> >     class Event: pass
> >
> > is an 'old-style' class.  Witness:
> >
> >     >>> class Event: pass
> >     ...
> >     >>> class NewEvent(object): pass
> >     ...
> >     >>> type(Event)
> >     <type 'classobj'>
> >     >>> type(NewEvent)
> >     <type 'type'>
> >     >>> type(Event())
> >     <type 'instance'>
> >     del>>> type(NewEvent())
> >     <class '__main__.NewEvent'>
> >
> > All old-style classes are actually objects of type 'classobj' (they
> > all have the same type!), all their instances are all of type 'instance'.
>
> Oops somthing disappeared in the copy/paste process:
>
>    >>> class FooBar: pass
>    ...
>
> >     >>> type(FooBar) == type(Event)
> >     True
> >     >>> type(FooBar()) == type(Event())
> >     True
> >
> > Whereas instances of new-style classes are of type their class:
> >
> >     >>> class NewFooBar(object): pass
> >     ...
> >     >>> type(NewFooBar) == type(NewEvent)
> >     True
> >     >>> type(NewFooBar()) == type(NewEvent())
> >     False
> >
> > However, in python 2.X (X > 2?), you can force all classes to of a
> > certain type by setting the global variable '__metaclass__'. So:
> >
> >     >>> type(Event) # Event is an old-style class
> >     <type 'classobj'>
> >     >>> __metaclass__ = type
> >     >>> class Event: pass
> >     ...
> >     >>> type(Event) # Now Event is new-style!
> >     <type 'type'>
> >
> > HTH
> >
> > --
> > Arnaud
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards
Alok Kumar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080529/c832829f/attachment-0001.html>


More information about the Python-list mailing list