473,698 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a class name

Hi,

How does one get the name of a class from within the class code? I
tried something like this as a guess:

self.__name__

Obviously it didn't work.

Anyone know how to do that?

Thanks,

Harlin

Feb 17 '07
14 2797
Gabriel Genellina a écrit :
En Sun, 18 Feb 2007 14:14:41 -0300, goodwolf <Ro**********@g mail.com>
escribió:
>On Feb 18, 9:17 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
>>En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Ka...@g mail.com>
escribió:

I suppose that you wont get class name into its code (or before
definition end) but not into a method definition.

import sys

def getCodeName(dea p=0):
return sys._getframe(d eap+1).f_code.c o_name

class MyClass (object):
name = getCodeName() + '!'

What's the advantage over MyClass.__name_ _?

--
Gabriel Genellina

>>>>class C(object):

... name = C.__name__
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in C
NameError: name 'C' is not defined
>>>>>

I were asking, why do you want a "name" attribute since "__name__"
already exists and has the needed information. And worst, using an
internal implementation function to do such task.
This might be useful to avoid metaclass hacks when trying to initialize
a class attribute that would require the class name. (my 2 cents)
Feb 18 '07 #11
On Feb 18, 11:54 pm, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
Gabriel Genellina a écrit :
En Sun, 18 Feb 2007 14:14:41 -0300, goodwolf <Robert.Ka...@g mail.com>
escribió:
On Feb 18, 9:17 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
>En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Ka...@g mail.com>
escribió:
I suppose that you wont get class name into its code (or before
definition end) but not into a method definition.
import sys
def getCodeName(dea p=0):
return sys._getframe(d eap+1).f_code.c o_name
class MyClass (object):
name = getCodeName() + '!'
>What's the advantage over MyClass.__name_ _?
>--
Gabriel Genellina
>>>class C(object):
... name = C.__name__
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in C
NameError: name 'C' is not defined
I were asking, why do you want a "name" attribute since "__name__"
already exists and has the needed information. And worst, using an
internal implementation function to do such task.

This might be useful to avoid metaclass hacks when trying to initialize
a class attribute that would require the class name. (my 2 cents)
It's still an ugly hack. :-)

(But a nice implementation detail to know about none-the-less.)

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml

Feb 19 '07 #12
En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <fu******@gmail .comescribió:
[somebody] wrote:
>def getCodeName(dea p=0):
return sys._getframe(d eap+1).f_code.c o_name
>class MyClass (object):
name = getCodeName() + '!'
>>What's the advantage over MyClass.__name_ _?
I were asking, why do you want a "name" attribute since "__name__"
already exists and has the needed information. And worst, using an
internal implementation function to do such task.

This might be useful to avoid metaclass hacks when trying to initialize
a class attribute that would require the class name. (my 2 cents)

It's still an ugly hack. :-)
(But a nice implementation detail to know about none-the-less.)
Having class decorators would be nice...

--
Gabriel Genellina

Feb 19 '07 #13
On Feb 19, 5:11 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <fuzzy...@gmail .comescribió:
[somebody] wrote:
def getCodeName(dea p=0):
return sys._getframe(d eap+1).f_code.c o_name
class MyClass (object):
name = getCodeName() + '!'
>What's the advantage over MyClass.__name_ _?
I were asking, why do you want a "name" attribute since "__name__"
already exists and has the needed information. And worst, using an
internal implementation function to do such task.
This might be useful to avoid metaclass hacks when trying to initialize
a class attribute that would require the class name. (my 2 cents)
It's still an ugly hack. :-)
(But a nice implementation detail to know about none-the-less.)

Having class decorators would be nice...
Yes - and Guido said he wouldn't be opposed to the idea when it was
poitned out that they would be very useful for both IronPython and
Jython to implement features of their underlying platforms.

I haven't heard anyone (other than you) mention them recently
though...

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml
--
Gabriel Genellina

Feb 19 '07 #14
En Mon, 19 Feb 2007 17:25:56 -0300, Fuzzyman <fu******@gmail .comescribió:
On Feb 19, 5:11 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
>En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <fuzzy...@gmail .com>
escribió:
[somebody] wrote:
def getCodeName(dea p=0):
return sys._getframe(d eap+1).f_code.c o_name
>This might be useful to avoid metaclass hacks when trying to
initialize
>a class attribute that would require the class name. (my 2 cents)
It's still an ugly hack. :-)
(But a nice implementation detail to know about none-the-less.)

Having class decorators would be nice...

Yes - and Guido said he wouldn't be opposed to the idea when it was
poitned out that they would be very useful for both IronPython and
Jython to implement features of their underlying platforms.

I haven't heard anyone (other than you) mention them recently
though...
Maybe it's a sign that actually nobody cares about class decorators :(

--
Gabriel Genellina

Feb 20 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6916
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This inteface has a lot more methods which works fine, it is just the
8
1410
by: abbylee26 | last post by:
User enters account number if another account number is needed User clicks add account which creates another row When validating when they hit submit I want to check to make sure they tell us how much money to charge to all account numbers except the first one (all remaining money goes on the first account number) within the header script:
12
2014
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc.., which are suppose to describe the structure of my main TextFile class. Also i have created some custom collection classes, which only take items of the types, they are designed for. Now the problem is that I want my element classes, like...
0
3713
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
20
27781
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of getting 'l_Fred_my_ins' out of the following. .... My_cls l_Fred_my_ins = new My_cls();
2
2710
cassbiz
by: cassbiz | last post by:
I am using strtotime and I have read up on some examples and am getting the wrong output, it jumps by several days instead of one day at a time. Ultimately what I am trying to accomplish is to set up an arrival time and a departure time for the script. This is using AJAX (which I am such a newbee @) from my earlier post http://www.thescripts.com/forum/threadnav583131-1-10.html Any help is greatly appreciated. <? $output = '';
1
3195
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as follows: The code below is for the first page:session_start is in line 3 <link href="css/jobSheet.css" rel="stylesheet" type="text/css" /> session_start();
0
2916
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form action="/login"> <html:text property="userName" value="Bunty"/> <html:submit/>
2
6653
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double) Location: class Week5MortgageGUI Week5MortgageLogic allint = logic.allInterest(amount, term, rate); Week5MortgageGUI.java:152:cannot find symbol symbol: method allInterest(double,double,double) Location: class Week5MortgageGUI
5
1632
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace MyFunctions Public Class BitHandling
0
9157
Oralloy
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...
0
9023
jinu1996
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...
1
8893
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,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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();...
0
4366
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...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1999
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.