相关疑难解决方法(0)

为什么没有构造函数参数的类需要括号

我开始学习PHP中的编程/ OOP。据我对PHP最佳实践的了解,如果不带任何参数,则可以实例化没有括号的类。

$class = new Class;
Run Code Online (Sandbox Code Playgroud)

相对于:

$class = new Class();
Run Code Online (Sandbox Code Playgroud)

我开始将自己的技能扩展到python中,并于昨天浪费了大约5个小时,试图弄清为什么一个函数即使简单得很简单也不会传递参数。我的代码:

class MainViewWidgets(MainViewContainer):

    def __init__(self):
        # instantiating like this prevents MainViewController.getHeaderItems from returning the arg passed to it, however the code still "works" in some sense
        self.controller = MainViewController 

        #this works
        self.controller = MainViewController()

    def createHeaderOptionCheckbox(self, pane):
        self.header_string = StringVar()
        header_checkbox = ttk.Checkbutton(pane, text='Data Contains Headers', variable=self.header_string, onvalue='headers', offvalue='keys')
        self.header_string.trace('w', self.headerOptionCheckboxChanged)
        return header_checkbox

    def headerOptionCheckboxChanged(self, *args):
        print(self.header_string.get())
        #will print "headers" or "keys" on checkbox toggle
        print(self.controller.getHeaderItems(self.header_string.get()))
        #prints "default" …
Run Code Online (Sandbox Code Playgroud)

python oop tkinter

1
推荐指数
1
解决办法
1416
查看次数

Python中添加的方法

在Ruby中,可以为在对象上定义方法时创建回调:

module Chatty
  def self.method_added(method_name)
    puts "Adding #{method_name.inspect}"
  end
  def self.some_class_method() end
  def some_instance_method() end
end
# => Adding :some_instance_method
Run Code Online (Sandbox Code Playgroud)

Python有没有类似的回调?

python methods class python-3.x

1
推荐指数
1
解决办法
102
查看次数

Python 中的“pass”是什么类型?

pass语句通常是未来代码的占位符,并用作操作null

但为什么它没有type在Python中定义a。不应该属于None类型吗?

python

1
推荐指数
1
解决办法
393
查看次数

'__main__' 在 type() 的输出中是什么意思

如果我创建一个如下所示的类,并检查对象的类型,我会得到以下输出。

我的问题是__main__这里是什么意思?

class Student(object):
    pass

>>>a = Student()
>>>type(a)
<class '__main__.Student'>
Run Code Online (Sandbox Code Playgroud)

还有一个问题,如果我检查 Student 类的类型,我会得到以下输出。

>>>type(Student)
<class 'type'>
Run Code Online (Sandbox Code Playgroud)

<class 'type'>这里是什么意思?

python python-2.7 python-3.x

1
推荐指数
1
解决办法
1768
查看次数

“class Meta:”在 Django 和 Django REST Framework 中做什么?

我试图弄清楚class Meta:Django 到底做了什么。

我在 DRF 中遇到了下面的代码,但不确定为什么下面class Meta:model = Userfields = [...]。对创建数据库有帮助吗?

from django.contrib.auth.models import User, Group
from rest_framework import serializers


class UserSerializer(
        serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'groups']
Run Code Online (Sandbox Code Playgroud)

class Meta:Django 中使用的有什么不同,如下所示。

from django.db import models

class Ox(models.Model):
    horn_length = models.IntegerField()

    class Meta:
        ordering = ["horn_length"]
        verbose_name_plural = "oxen"
Run Code Online (Sandbox Code Playgroud)

我试图从 Django 和 DRF 文档中获得进一步的理解,但是我没有看到DRF的解释model = ...和使用。fields = [...]class Meta

metadata class django-models django-views django-rest-framework

1
推荐指数
1
解决办法
764
查看次数

为什么MyClass .__ class__返回的值不同于MyClass().__ class__?

我希望能够打电话给'person.Person. "并取回'类person.Person’,像它用于一个Person对象:

>>> p = person.Person()
>>> p.__class__
<class 'person.Person'>
>>> person.Person.__class__
<class 'perframe.datatype.TypeSystemMeta'>
Run Code Online (Sandbox Code Playgroud)

这是Person继承树......

class TypeSystem(object):
    __metaclass__ = TypeSystemMeta    

class Model(TypeSystem):
    pass    

class Node(Vertex,Model):
    pass

class Person(Node):
    pass
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

python reflection inheritance multiple-inheritance method-resolution-order

0
推荐指数
1
解决办法
325
查看次数

"对象"是python中"类的实例"的同义词吗?

我开始学习Python并且遇到了课程(没有先前的编程背景或知识).

"对象"是"类的实例"的同义词吗?

谢谢.

python class object instances

0
推荐指数
1
解决办法
1131
查看次数

Python什么时候在内存中创建类对象?

我们来看看下面的例子:

>>> class Foo(object):
...     pass
... 
Run Code Online (Sandbox Code Playgroud)

我目前的理解是当Python解释器读取行class Foo(object)[Foo类定义]时,它将在内存中创建一个Foo类对象.

然后我做了以下两个测试:

>>> dir()
['Foo', '__builtins__', '__doc__', '__name__', '__package__']
Run Code Online (Sandbox Code Playgroud)

看起来Python解释器在内存中存储了'Foo'类对象.

>>> id(Foo)
140608157395232
Run Code Online (Sandbox Code Playgroud)

看来Foo类对象是在内存地址:140608157395232.

我的推理是否正确?如果没有,Python什么时候在内存中创建类对象?

python memory class object

0
推荐指数
1
解决办法
170
查看次数

使用int作为基类时解释此错误

我知道这样做是错误的,但是我试着了解幕后发生的事情(如果你可以用鸭子类型来扩展它).代码如下:

>>> a = 2
>>> class C(a):
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() takes at most 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)

我试图理解为什么int()在这个例子中会被调用.这是类型,a但我不明白为什么这在这里很重要.

python inheritance class

0
推荐指数
1
解决办法
37
查看次数

我可以在 Python 中动态实现类方法吗?

在 Python 中,您可以动态实现类实例的属性,这已不是什么秘密。看起来是这样的:

class MyClass(object):
    def __getattribute__(self, name):
        if name == 'dynamic_attribute':
            return "dynamic attribute value"
        return super().__getattribute__(name)

# Create an instance of my class
obj = MyClass()

# I've implemented the 'dynamic_attribute' attribute dynamically, so I can
# reference it without ever having assigned it a value
print(obj.dynamic_attribute)

# As I've only implemented that one attribute dynamically, other attributes
# behave in the standard way
try:
    print(obj.regular_attribute)
except AttributeError as ex:
    print(f"EXPECTED ERROR: {ex}")
obj.regular_attribute = "regular attribute value" …
Run Code Online (Sandbox Code Playgroud)

python metaclass class dynamic

0
推荐指数
1
解决办法
191
查看次数

使用元类在类上键入函数

class ParamMeta(type):
  def __str__(self):
    return self.__name__


class Param(object):
  __metaclass__=ParamMeta

class SomeParam(Param):
  pass
Run Code Online (Sandbox Code Playgroud)

我想要的是:

type(SomeParam)==Param
Run Code Online (Sandbox Code Playgroud)

我该如何实现这一目标?

更新:我需要更改以获得所需的行为?

更新2:对于后人:这个问题完全是完全虚假的.请假装你没看到它;)

python metaclass

-1
推荐指数
1
解决办法
94
查看次数