[Python-de] Klassenmethoden

19/07/2013 - 15:23 von Wagner, Frank | Report spam

Hallo ich hab folgendes Problem:

Ich möchte einige Methoden einer Klasse verwenden ohne zuvor eine Instanz der Klasse anlegen zu müssen.

Wie ich in Erfahrung gebracht habe ist dies mit dem Schlüsselwort @classmethod vor der eigentlichen Methode möglich wenn diese Methode ihre Zugriffe über cls und nicht self referenziert.

Offensichtlich ist es mir allerdings nicht möglich eine Methode der Klasse hiermit zu verwenden die sich auf eine Instanz bezieht also mit def foo(self, ...)
erstellt ist?

Gibt es eine Möglichkeit dies zu lösen oder muss ich jetzt alle Methoden der Klasse als Klassenmethoden ausweisen und cls statt self verwenden? Hierbei würde es allerdings wenig Sinn machen noch eine Klasse zu definieren, da könnte ich ja auch alle Methoden direkt in ein modulfile schreiben.

Hier ein kleines Beispiel was ich meine:

<<
class ModulTest():
''' Kurze Beschreibung der Klasse
und ihrer Mehtoden
'''
Liste= []
String = "0"
fl_val = 0.0
int_val = 0

def __init__(self):
''' Kurze Beschreibung der Funktion
Kurze Beschreibung der Argumente
Kurze Beschreibung der Rueckgabewerte
'''
pass

def verarbeite(self, Zahl):
self.String = str(Zahl)
for i in self.String:
self.Liste.append(i)

@classmethod
def getOutput(cls, zahl):
cls.verarbeite(zahl)
print cls.String









Aufgerufen soll es wie folgt werden:


From modultest import ModulTest as mt


mt.getOutput(12)

Schon jetzt vielen Dank für Eure Ratschlàge


Gruß

Frank



Bevor Sie diese E-Mail ausdrucken, überlegen Sie bitte, ob dies wirklich erforderlich ist. Please think before you print.
WIKA Alexander Wiegand SE & Co. KG
Alexander-Wiegand-Straße 30 - 63911 Klingenberg - Germany
Kommanditgesellschaft: Sitz Klingenberg - Amtsgericht Aschaffenburg HRA 1819
Komplementàrin: WIKA Verwaltungs SE & Co. KG - Sitz Klingenberg -
Amtsgericht Aschaffenburg HRA 4685
Komplementàrin: WIKA International SE - Sitz Klingenberg -
Amtsgericht Aschaffenburg HRB 10505
Vorstand: Alexander Wiegand
Vorsitzender des Aufsichtsrats: Dr. Max Egli
Telefon: (09372) 132-0
Internet: www.wika.com
The information contained in this E-Mail and any attached files are strictly confidential and may be subject to legal privilege. If you are not the intended recipient, his representative or the person responsible for delivering the message to the intended recipient, be advised that you have received this message in error and that any dissemination, copying or use of this message or attachment is strictly forbidden, as is the disclosure of the information therein. If you have received this E-Mail in error, please notify us immediately by E-Mail or telephone and delete this message and all its attachments subsequently. All reasonable precautions have been taken to ensure no viruses are present in this E-Mail.


<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office...omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.E-MailFormatvorlage17
{mso-style-type:personal-compose;
font-family:"Arial","sans-serif";
color:black;
font-weight:normal;
font-style:normal;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
{page:WordSection1;}
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="DE" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Hallo ich hab folgendes Problem:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Ich möchte einige Methoden einer Klasse verwenden ohne zuvor eine Instanz der Klasse anlegen zu müssen.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Wie ich in Erfahrung gebracht habe ist dies mit dem Schlüsselwort @classmethod vor der eigentlichen Methode möglich wenn diese Methode ihre Zugriffe über cls und
nicht self referenziert.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Offensichtlich ist es mir allerdings nicht möglich eine Methode der Klasse hiermit zu verwenden die sich auf eine Instanz bezieht also mit def foo(self, &#8230;)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">erstellt ist?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Gibt es eine Möglichkeit dies zu lösen oder muss ich jetzt alle Methoden der Klasse als Klassenmethoden ausweisen und cls statt self verwenden? Hierbei würde es
allerdings wenig Sinn machen noch eine Klasse zu definieren, da könnte ich ja auch alle Methoden direkt in ein modulfile schreiben.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Hier ein kleines Beispiel was ich meine:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&lt;&lt;<o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">class ModulTest():<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; ''' Kurze Beschreibung der Klasse<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; und ihrer Mehtoden<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; '''<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; Liste= []<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; String = &quot;0&quot;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; fl_val = 0.0<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; int_val = 0<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; def __init__(self):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ''' Kurze Beschreibung der Funktion<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Kurze Beschreibung der Argumente<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Kurze Beschreibung der Rueckgabewerte<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '''<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; pass<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; def verarbeite(self, Zahl):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">self.String = str(Zahl)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in self.String:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">self.Liste.append(i)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; @classmethod<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp; def getOutput(cls, zahl):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cls.verarbeite(zahl)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print cls.String<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">&gt;&gt;<o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Aufgerufen soll es wie folgt werden:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">From modultest import ModulTest as mt<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">mt.getOutput(12)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Schon jetzt vielen Dank für Eure Ratschlàge<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Gruß<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black">Frank<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:black"><o:p>&nbsp;</o:p></span></p>
</div>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">--</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><A href="http://en-co.wika.de/PSM02_en_co.WI...gt;<IMG src="http://en-co.wika.de/upload/PIC_EB_....gif" align="baseline" alt="" hspace="0" border="0"></A></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><A href="http://en-co.wika.de/PSM02_en_co.WI...</P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">--</FONT></P><SPAN style="FONT-FAMILY: Webdings; COLOR: #007f00; FONT-SIZE: 18pt"><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: #007f00; FONT-SIZE: 8pt">
<P class="MsoNormal" style="MARGIN: 0in 0in 0pt"><SPAN><FONT face="Webdings" size="5"><FONT face="Arial" size="1">&nbsp;</FONT><FONT size="4">P</FONT></FONT><FONT size="1">&nbsp;&nbsp;</FONT><FONT size="2"><STRONG><EM> <FONT size="1">Bevor Sie diese E-Mail ausdrucken, überlegen Sie bitte, ob dies wirklich erforderlich ist. Please think before you print.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><O></O:P></FONT></EM></STRONG></FONT></SPAN></P>
<P class="MsoNormal" style="MARGIN: 0in 0in 0pt"></SPAN></SPAN><FONT face="Arial" size="1">--</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1"><STRONG>WIKA Alexander Wiegand SE &amp; Co. KG</STRONG></FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1"><STRONG>Alexander-Wiegand-Straße 30 - 63911 Klingenberg - Germany</STRONG></FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Kommanditgesellschaft: Sitz Klingenberg - Amtsgericht Aschaffenburg HRA 1819</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Komplementàrin: WIKA&nbsp;Verwaltungs&nbsp;SE &amp; Co. KG&nbsp;- Sitz Klingenberg -</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Amtsgericht Aschaffenburg HRA 4685</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Komplementàrin: WIKA International SE - Sitz Klingenberg -</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Amtsgericht Aschaffenburg HRB 10505</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Vorstand: Alexander Wiegand</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Vorsitzender des Aufsichtsrats: Dr. Max Egli</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Telefon: (09372) 132-0</FONT></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">Internet: </FONT><A href="http://www.wika.com"><FONT face="Arial" size="1">www.wika.com</FONT></A></P>
<P class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><FONT face="Arial" size="1">-- </FONT></P>
<DIV><FONT face="Arial" size="1">The information contained in this E-Mail and any attached files are strictly confidential and may be subject to legal privilege. If you are not the intended recipient, his representative or the person responsible for delivering the message to the intended recipient, be advised that you have received this message in error and that any dissemination, copying or use of this message or attachment is strictly forbidden, as is the disclosure of the information therein. If you have received this E-Mail in error, please notify us immediately by E-Mail or telephone and delete this message and all its attachments subsequently. All reasonable precautions have been taken to ensure no viruses are present in this E-Mail.</FONT></DIV></body>
</html>

 

Lesen sie die antworten

#1 Christoph Kliemt
19/07/2013 - 16:07 | Warnen spam
"Wagner, Frank" writes:

Hallo ich hab folgendes Problem:

Ich möchte einige Methoden einer Klasse verwenden ohne zuvor eine
Instanz der Klasse anlegen zu müssen.



Sowas hier?

http://docs.python.org/2/library/fu...aticmethod

Gruss, Christoph

Ähnliche fragen