Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:45:13+00:00 2026-05-23T11:45:13+00:00

In the following code, Graph() is acting as a proxy to Vertex and Edge

  • 0

In the following code, Graph() is acting as a proxy to Vertex and Edge — clients only access Vertex and Edge through Graph():

from rest import Resource
from elements import Vertex, Edge

class Graph(object):
    def __init__(self,db_url):
        self.resource = Resource(db_url)
        self.vertices = Vertex
        self.edges = Edge

g1 = Graph('http://localhost/one')   
g2 = Graph('http://localhost/two')

What are the best ways for Vertex and Edge to access the resource object, without having to pass it as a param to Vertex and Edge?

One of the reasons I don’t want to pass it as a param is because Vertex and Edge have classmethods, such as create(), that need access to the resource object too.

Flask/Werkzeug uses “context locals” (http://werkzeug.pocoo.org/docs/local/) — is that the right approach here, or is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:45:13+00:00Added an answer on May 23, 2026 at 11:45 am

    If your resource object is unique, could you make it a singleton? The fact that you want to use it from a class method makes me think that it’s probably the case. If its only purpose is to provide the database connection, could you consider using a connection pool?

    If you still need to pass it to your classes, you can simply assign it to class attributes.

    class Vertex(object):
        @classmethod
        def foo(cls):
            print cls.resource
    
    Vertex.resource = 'something'
    v = Vertex()
    v.foo()
    

    This can also be done in __init__:

    class Vertex(object):
    
        def __init__(self, resource):
            if not hasattr(self.__class__, 'resource'):
                self.__class__.resource = resource
    
        @classmethod
        def foo(cls):
            print cls.resource
    
    resource = 'some resource'
    v = Vertex(resource)
    v.foo()
    

    But really my intuition is that you should look into using a singleton, which in many cases can be implemented in Python simply as a module.

    Finally if I can make a couple of remarks about your code, I find it confusing that you’re assigning classes to plural variable names. When I see self.edges I would expect a collection or an iterable, not a class. I also wonder why you would want a class method called create. What does it do that __init__ cannot do?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code from graph theory topics,kruskal algorithm for minimal spanning tree #include<iostream>
In my the following code, I am traversing a graph through breadth first search
I drew this graph using matplotlib using the following code. import matplotlib import matplotlib.pyplot
Following code iterates through many data-rows, calcs some score per row and then sorts
The following code should find the appropriate project tag and remove it from the
I have the following code: import matplotlib.pyplot as plt cdict = { 'red' :
I have tried following code for creating sparse graph in MATLAB: cm = sparse([1
I have the following C# code: public JsonResult Graph() { var result = new
I have the following code to bring up a jqplot graph in the head:
UPDATE: The following code gives me an error Graph.cpp: In function 'std::ostream& operator<<(std::ostream&, const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.