I've come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.
The code is as follows:
Person = struct.new( :name, :birthday, :children)
I tried something like this, but it's nothing close to what I'd like:
def klass(table, *args):
cls = new.classobj(ta ble, (), {})
for i in args:
setattr(cls, i, i)
return cls
But this above is not what I want.
I guess I should find a way to include the constructor code inside
this function, but I don't know if this is possible.
Also, I wonder if there is a way to use the variable name in order to
create a class with the same name (as in "Person"abo ve).
Well, if anyone has an idea, I'd like to know...
Luis 28 1693
Luis M. González a écrit :
I've come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.
The code is as follows:
Person = struct.new( :name, :birthday, :children)
s/struct/Struct/
I tried something like this, but it's nothing close to what I'd like:
def klass(table, *args):
cls = new.classobj(ta ble, (), {})
for i in args:
setattr(cls, i, i)
return cls
But this above is not what I want.
I guess I should find a way to include the constructor code inside
this function, but I don't know if this is possible.
Also, I wonder if there is a way to use the variable name in order to
create a class with the same name (as in "Person"abo ve).
Well, if anyone has an idea, I'd like to know...
Here's a *very* Q&D attempt - that doesn't solve the name problem:
def Struct(name, *attribs):
args = ", ".join("%s=None " % attr for attr in attribs)
body = "\n ".join("sel f.%s = %s" % (attr, attr) \
for attr in attribs)
source = ("""
class %s(object):
def __init__(self, %s):
%s
""".strip() ) % (name, args, body)
#print source
code = compile(source, 'dummy', 'single')
exec code
return locals()[name]
But note that I'd immediatly fire anyone using such an abomination in
production code.
Luis M. González wrote:
I've come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.
The code is as follows:
Person = struct.new( :name, :birthday, :children)
How about something like::
class Person(Record):
__slots__ = 'name', 'birthday', 'children'
You can then use the class like::
person = Person('Steve', 'April 25', [])
assert person.name == 'Steve'
assert person.birthday == 'April 25'
assert not person.children
Is that what you were looking for? If so, the recipe for the Record
class is here: http://aspn.activestate.com/ASPN/Coo.../Recipe/502237
STeVe
On Feb 28, 6:21 pm, Steven Bethard <steven.beth... @gmail.comwrote :
Luis M. González wrote:
I've come across a code snippet inwww.rubyclr.c omwhere they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.
The code is as follows:
Person = struct.new( :name, :birthday, :children)
How about something like::
class Person(Record):
__slots__ = 'name', 'birthday', 'children'
You can then use the class like::
person = Person('Steve', 'April 25', [])
assert person.name == 'Steve'
assert person.birthday == 'April 25'
assert not person.children
Is that what you were looking for? If so, the recipe for the Record
class is here:
http://aspn.activestate.com/ASPN/Coo.../Recipe/502237
STeVe
Hmmm... not really.
The code above is supposed to be a shorter way of writing this:
class Person:
def __init__(self, name, birthday, children):
self.name = name
self.birthday = birthday
self.children = children
So the purpose of this question is finding a way to emulate this with
a single line and minimal typing.
There are a few problems here:
1) How to get the variable name (in this case "Person") become the
name of the class without explicity indicating it.
2) How to enter attribute names not enclosed between quotes. The only
way I can do it is by entering them as string literals.
It's not that I desperately need it, but I'm just curious about it...
Luis
Luis M. González wrote:
On Feb 28, 6:21 pm, Steven Bethard <steven.beth... @gmail.comwrote :
>How about something like::
class Person(Record): __slots__ = 'name', 'birthday', 'children'
You can then use the class like::
person = Person('Steve', 'April 25', []) assert person.name == 'Steve' assert person.birthday == 'April 25' assert not person.children
Is that what you were looking for? If so, the recipe for the Record class is here:
http://aspn.activestate.com/ASPN/Coo.../Recipe/502237
[snip]
Hmmm... not really.
The code above is supposed to be a shorter way of writing this:
class Person:
def __init__(self, name, birthday, children):
self.name = name
self.birthday = birthday
self.children = children
So the purpose of this question is finding a way to emulate this with
a single line and minimal typing.
That __init__ is exactly what was generated in my example above. So
you're mainly objecting to using two-lines? You can make it a one-liner
by writing::
class Person(Record): __slots__ = 'name', 'birthday', 'children'
1) How to get the variable name (in this case "Person") become the
name of the class without explicity indicating it.
The only things that know about their own names are class statements
(through metaclasses) so you can't really do it without a class
statement of some sort (which means you'll have to use two lines).
2) How to enter attribute names not enclosed between quotes. The only
way I can do it is by entering them as string literals.
If you're really bothered by quotes, a pretty minimal modification to
the recipe could generate the same code from:
class Person(Record): slots = 'name birthday children'
STeVe
Luis M. González wrote:
I've come across a code snippet in www.rubyclr.com where they show
how easy it is to declare a class compared to equivalent code in
c#. I wonder if there is any way to emulate this in Python.
The code is as follows:
Person = struct.new( :name, :birthday, :children)
What's easy about this?
Also, this is a definition and not just a declaration.
But this above is not what I want.
I guess I should find a way to include the constructor code inside
this function, but I don't know if this is possible.
Could you please describe what exactly you want in an abstract way?
Also, I wonder if there is a way to use the variable name in order
to create a class with the same name (as in "Person"abo ve).
Two classes with the same name?
In Python, classes have no name. They are anonymous objects which
can be bound to names.
Regards,
Björn
--
BOFH excuse #367:
Webmasters kidnapped by evil cult.
Bjoern Schliessmann a écrit :
(snip)
In Python, classes have no name.
class Toto(object):
pass
print Toto.__name__
Bruno Desthuilliers wrote:
class Toto(object):
pass
print Toto.__name__
Okay, I revoke my statement and assert the opposite.
But what's it (__name__) good for?
Regards,
Björn
--
BOFH excuse #179:
multicasts on broken packets
In <54************ *@mid.individua l.net>, Bjoern Schliessmann wrote:
Bruno Desthuilliers wrote:
>class Toto(object): pass
print Toto.__name__
Okay, I revoke my statement and assert the opposite.
But what's it (__name__) good for?
As objects don't know to which name they are bound, that's a good way to
give some information in stack traces or when doing introspection.
Ciao,
Marc 'BlackJack' Rintsch
On Mar 1, 9:40 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
In <54msaoF21c6h.. .@mid.individua l.net>, Bjoern Schliessmann wrote:
But what's it (__name__) good for?
As objects don't know to which name they are bound, that's a good way to
give some information in stack traces or when doing introspection.
Also, the name is used by pickle to find the class of pickled
instances.
Michele Simionato This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Zatvornitskiy |
last post by:
Hello All!
I'am novice in python, and I find one very bad thing (from my point of view) in
language. There is no keyword or syntax to declare variable, like 'var' in
Pascal, or special syntax in C. It can cause very ugly errors,like this:
epsilon=0
S=0
while epsilon<10:
S=S+epsilon
|
by: Xiangliang Meng |
last post by:
Hi, all.
What are the benefit and the drawback of defining a class embedded inside
another class?
For example:
class List
{
public:
|
by: I_AM_DON_AND_YOU? |
last post by:
There is one more problem I am facing but didn't get the solution. In my
Setup Program I am not been able to create 2 things (when the program is
intalled on the client machine ) : (1) create shortcut to my program/utility
(2) Entry in Windows' Start --> Program Menu.
Actually in my VB.Net solution I have two projects (1) MYPROGRAM (2)
MYPROGRAM_INSTALLER. MYPROGRAM is a "Windows Application".
MYPROGRAM_INSTALLER is a "SetUp Wizard"...
|
by: mark.moore |
last post by:
I know this has been asked before, but I just can't find the answer in
the sea of hits...
How do you forward declare a class that is *not* paramaterized, but is
based on a template class?
Here's what I thought should work, but apparently doesn't:
class Foo;
void f1(Foo* p)
|
by: Mike Edgewood |
last post by:
Is there a simple way, shortcut, or macro to create a property
skeletons?
I would live to be able to type a name and a type and have the property
created based on that info.
Private mLastName as String
Public Property LastName() as String
Get
Return mLastName
| |
by: zfareed |
last post by:
#include <iostream>
#include <fstream>
using namespace std;
template<class ItemType>
class SortedList
{
private:
|
by: Jess |
last post by:
Hello,
I was told that if I declare a static class constant like this:
class A{
static const int x = 10;
};
then the above statement is a declaration rather than a definition.
As I've *defined* "x"'s value to be 10, isn't above statement a
|
by: gasfusion |
last post by:
Hey guys!
I'm having some issues with one of my installer packages i compiled with Microsoft Visual Studio 2005. (VB installer is buggy as hell and wouldn't work on half of our machines no matter what we did with it) The installer wraps a VB 6.0 application. When i install the package on my machine everything works flawlessly. When i installed it on the user's machine, once again, everything ran great. When we uninstalled it and installed it...
|
by: mahesh.kanakaraj |
last post by:
Dear All,
I have a question to ask about the validity of a complexType
declaration.
Lets say an element declaration inside a schema looks like this....
<element name="abc" type="abcType">
and type declaration looks like,
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |