装饰的功能@staticmethod和装饰的功能有什么区别@classmethod?
是否有可能在python中有静态类变量或方法?这样做需要什么语法?
是否有可能在Python中使用静态方法,因此我可以在不初始化类的情况下调用它们,例如:
ClassName.static_method()
Run Code Online (Sandbox Code Playgroud) 我是python的新手,已经撞墙了.我遵循了几个教程,但无法通过错误:
Traceback (most recent call last):
File "C:\Users\Dom\Desktop\test\test.py", line 7, in <module>
p = Pump.getPumps()
TypeError: getPumps() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)
我检查了几个教程,但似乎与我的代码没有任何不同.我唯一能想到的是python 3.3需要不同的语法.
主要内容:
# test script
from lib.pump import Pump
print ("THIS IS A TEST OF PYTHON") # this prints
p = Pump.getPumps()
print (p)
Run Code Online (Sandbox Code Playgroud)
泵类:
import pymysql
class Pump:
def __init__(self):
print ("init") # never prints
def getPumps(self):
# Open database connection
# some stuff here that never gets executed because of error
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,"self"会自动传递给构造函数和方法.我在这做错了什么?
我使用的是Windows 8和python …
我正在阅读PEP 0008(样式指南),我注意到它建议使用self作为实例方法中的第一个参数,但cls是类方法中的第一个参数.
我已经使用并编写了几个类,但我从未遇到过类方法(嗯,一个将cls作为参数传递的方法).有人能告诉我一些例子吗?
谢谢!
我问这个问题是因为对这个答案的评论主题进行了讨论.我90%的方式来绕过它.
In [1]: class A(object): # class named 'A'
...: def f1(self): pass
...:
In [2]: a = A() # an instance
Run Code Online (Sandbox Code Playgroud)
f1 存在三种不同的形式:
In [3]: a.f1 # a bound method
Out[3]: <bound method a.f1 of <__main__.A object at 0x039BE870>>
In [4]: A.f1 # an unbound method
Out[4]: <unbound method A.f1>
In [5]: a.__dict__['f1'] # doesn't exist
KeyError: 'f1'
In [6]: A.__dict__['f1'] # a function
Out[6]: <function __main__.f1>
Run Code Online (Sandbox Code Playgroud)
绑定方法,未绑定方法和函数对象之间的区别是什么,所有这些都由f1描述?如何调用这三个对象?他们怎么能相互转化?关于这些东西的文档很难理解.
试图创建一个程序,当你点击按钮(生成代码)时,它从文件中提取一行数据并输出到
TypeError:generatecode()获取0个位置参数,但给出了1
from tkinter import *
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("COD:WWII Codes")
self.pack(fill=BOTH, expand=1)
codeButton = Button(self, text = "Generate Code", command = self.generatecode)
codeButton.place(x=0, y=0)
def generatecode(self):
f = open("C:/Programs/codes.txt", "r")
t.insert(1.0. f.red())
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 帮助我我在这里做错了什么,因为我收到以下错误,
类型错误:fizz_buzz() 采用 0 个位置参数,但给出了 1 个
class FizzBuzz:
def __init__(self, number_value):
self.number_value = number_value
def fizz_buzz():
if number_value % 3 == 0 and number_value % 5 == 0:
print("FizzBuzz")
elif number_value % 3 == 0:
print("Fizz")
elif number_value % 5 == 0:
print("Buzz")
else:
return f"{number_value} can't be multiplied by either 3 or 5"
number_value = int(input("Enter number: "))
fizzbuzz_object = FizzBuzz(number_value)
fizzbuzz_object.fizz_buzz()
Run Code Online (Sandbox Code Playgroud) 我有一个具有静态方法的类,我想在该类中使用另一个静态方法来调用该方法,但它返回NameError: name ''method_name' is not defined
我正在尝试做的事情的例子。
class abc():
@staticmethod
def method1():
print('print from method1')
@staticmethod
def method2():
method1()
print('print from method2')
abc.method1()
abc.method2()
Run Code Online (Sandbox Code Playgroud)
输出:
print from method1
Traceback (most recent call last):
File "test.py", line 12, in <module>
abc.method2()
File "test.py", line 8, in method2
method1()
NameError: name 'method1' is not defined
Run Code Online (Sandbox Code Playgroud)
解决这个问题的最佳方法是什么?
我想将代码保留为这种格式,其中有一个类包含这些静态方法并使它们能够相互调用。