装饰的功能@staticmethod和装饰的功能有什么区别@classmethod?
新的pycharm发行版(3.1.3社区版)建议将不能使用当前对象状态的方法转换为静态.

这有什么实际的原因?某种微观性能( - 或内存) - 优化?
我在该类中有一个类Person和一个静态方法,名为call_person:
class Person:
def call_person():
print "hello person"
Run Code Online (Sandbox Code Playgroud)
在python控制台中,我导入类Person并调用Person.call_person().但它给我的错误说'module' object has no attribute 'call_person'.任何人都可以让我知道为什么我收到此错误?
我来自Java背景,我是python的新手.我有几个脚本共享一些与读取和写入文件相关的应用程序独有的辅助函数.一些与阅读相关的功能,一些与写作相关.在搜索正确的方法时,我看到了这一点:Python中的静态方法?
他在回答中提到:
最后,谨慎使用static方法!在Python中很少需要使用静态方法,并且我已经看到它们多次使用,其中单独的"顶级"函数会更清晰.
我不太了解顶级函数,我不确定给出这个更好的简单示例:1)为具有静态读取器函数的读者创建一个类,为编写者创建相同的类或2)声明这些帮助器作为全球功能,为什么?
编辑:关于这个主题的真正好文章我刚刚找到http://tomayko.com/writings/the-static-method-thing
如何在Python中声明静态属性?
这里写的是我如何声明一个方法: Python中的静态方法?
我需要将UUID作为主键实现,但我不确定如何在Django中执行它.
我的代码
class LinkRenewAd(models.Model): # This model will generate the uuid for the ad renew link
def make_uuid(self):
return str(uuid.uuid1().int>>64)
uuid = models.CharField(max_length=36, primary_key=True, default=make_uuid, editable=False)
main = models.ForeignKey(Main)
expiration_date = models.DateTimeField()
date_inserted = models.DateTimeField(auto_now_add=True)
date_last_update = models.DateTimeField(auto_now=True)
Run Code Online (Sandbox Code Playgroud)
当我尝试在南方生成这个新模型时,我得到了错误:
TypeError: make_uuid() takes exactly 1 argument (0 given)
Run Code Online (Sandbox Code Playgroud) I am quite amateur in OOP concepts of python so I wanted to know are the functionalities of self of Python in any way similar to those of this keyword of CPP/C#.
我收到错误
get_indiceComercioVarejista() 缺少 1 个必需的位置参数:“请求”
尝试访问方法 get_indiceComercioVarejista 时。我不知道它有什么问题。
意见:
from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
import requests
import pandas as pd
from rest_framework.views import APIView
from rest_framework.response import Response
class ChartData(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
data = {
'customer' : 10,
'sales': 100
}
return Response(data)
def get_indiceComercioVarejista(self, request, format=None):
data = {
'customer' : 10,
'sales': 100
}
return Response(data)
Run Code Online (Sandbox Code Playgroud)
网址:
from django.conf.urls import url
from . import views
from …Run Code Online (Sandbox Code Playgroud) 我只是想知道,因为偶尔我会遇到一些代码,我认为它们作为静态方法很好,但编码人员没有为某些方法放置装饰器。对于其他方法,它们已经就位。
那么...也许他们在使用或不使用静态方法装饰器时有什么想法?这种方法有什么优点吗?或者他们只是因为懒而省略了装饰器?
谢谢。
可能重复:
Python中的静态方法?
我认为我的问题很简单,但更清楚的是我只是想知道,我有这个:
class MyBrowser(QWebPage):
''' Settings for the browser.'''
def __init__(self):
QWebPage.__init__(self)
pass
def userAgentForUrl(self, url=None):
''' Returns a User Agent that will be seen by the website. '''
return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15"
Run Code Online (Sandbox Code Playgroud)
以及在同一个文件中的某个地方,我希望得到这个用户代理.
mb = MyBrowser()
user_agent = mb.userAgentForUrl()
print user_agent
Run Code Online (Sandbox Code Playgroud)
我试图做这样的事情:
print MyBrowser.userAgentForUrl()
Run Code Online (Sandbox Code Playgroud)
但得到了这个错误:
TypeError: unbound method userAgentForUrl() must be called with MyBrowser instance as first argument (got nothing instead)
Run Code Online (Sandbox Code Playgroud)
所以我希望你得到我所要求的东西,有时候我不想创建一个实例,而是从这种函数中检索数据.所以问题是可以做或不做,如果是,请给我一些指导如何实现这一目标.
我很确定已多次询问过这个问题,但我仍然不确定如何在Python中实现多个构造函数.我知道在python中,我只能有一个不同于java或C#或C++的构造函数.我还是很新的.长话短说,我需要实现一个线对象.该线将由函数y = ax + b表示.因此,我需要在行中存储的唯一内容是a,b和布尔值,用于特殊情况,其中行是垂直的(a =无穷大).在这种情况下,a将存储该行的x位置.要创建一条线,我有三种方法.1是直接放入a,b和布尔值.2是以元组的形式输入2个点.3是放入点和矢量.我的代码到目前为止:
class line:
def __init__(self, a, b, noSlope):
self.a = a
self.b = b
self.noSlope = noSlope
def lineFromPoints(point1, point2):
deltaX = point2[0] - point1[0]
deltaY = point2[1] - point1[1]
if deltaX == 0:
return line(point1[0], 0, True)
else:
a = deltaY / deltaX
b = point1[1] - a * point1[0]
return line(a, b, False)
def lineFromVector(vector, point):
if vector[0] == 0:
return line(point1[0], 0, True)
else:
a = vector[1] / vector[0]
b = point1[1] …Run Code Online (Sandbox Code Playgroud) 来自3.数据模型:
\n\n\n实例方法
\n实例方法对象组合了类、类实例和任何\n可调用对象(通常是用户定义的\xef\xac\x81ned 函数)。
\n
如果是定义的话,它的含义是什么?
\n如果不是定义,那么“实例方法”的定义是什么?
\n“实例方法”与类的方法是同一概念吗?
\n既然有人提出了类方法和静态方法、绑定方法和非绑定方法,那么我澄清一下:
\n我理解类的方法可以是普通方法、类方法或静态方法。我了解通过类或其实例访问的类的方法可以被绑定或函数。我从未听说过“实例方法”。即使看了引用,我也不知道它是什么,也不确定它是否与普通方法、类方法、静态方法、绑定方法或函数相关。
\n使用此代码,我想生成一个随机数元组。我知道有一些简单的方法可以在不使用类的情况下做到这一点,但我希望代码也有类。
import random
class Dice:
def roll(self):
generate = random.randint(1, 6)
generate2 = random.randint(1, 6)
return generate, generate2
dice = Dice
print(dice.roll())
Run Code Online (Sandbox Code Playgroud)
它生成此错误:
print(dice.roll())
TypeError: roll() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)
当我像这样更改我的代码时:print(dice.roll(self)) 它会创建另一个错误,即未定义自我名称。